简体   繁体   中英

error calling a php class function

I'm trying to call a function in a external php class file

control.php

require_once ('../vista/ViewEscola.php');
$a = new A();
$a->foo();

and this is the external file ViewEscola.php

class A
{
    public function foo()
    {
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

it doesn't do anything,

can anyone help me?

Instead of echo try return from the function and also pass the variable as an argument of function.Also dont use $this .If you are not getting any errors try display errors on with ini_set('display_errors', true'); error_reporting(E_ALL); ini_set('display_errors', true'); error_reporting(E_ALL); Try like

require_once ('../vista/ViewEscola.php');
class A
{
    public function foo($arg)
    {
        if (isset($this)) {
            $ret = '$arg is defined (';
            $ret .= get_class($arg);
            $ret .= ")\n";
        } else {
            $ret =  "\$arg is not defined.\n";
        }
        return $ret;
    }
}

$arg = 'Hi';
$a = new A();
$ret = $a->foo($arg);
echo $ret;
     <?php
    class A
   {
      public function foo()
     {
        if (isset($this)) {
        echo '$this is defined (';
        echo get_class($this);
        echo ")\n";
    } else {
        echo "\$this is not defined.\n";
    }
}
}
  $a = new A();
  $a->foo();
  ?>

I have tried executing the above code snippet into a php and it printed "$this is defined (A) " So might there be issue related to include path for class file into your code snippet

You can identify the error with following code snippet

 <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL);
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM