无法使用OOP修复一些错误:与构造函数调用函数有关

I am really new to OOP, and I am getting some errors with my first implementation of the __constructor method. The two errors are:

Notice: Undefined variable: _test in...

Fatal error: Cannot access empty property in ...

Here is my code:

<?php


class Info
{
    private static $_test;

    public function __construct()
    {
        $this->setIt(); //call setIt so $_test can be set to true or false for use by other functions in the class.
    }
    public function setIt()
    {
        if ("test" == "something") //simplified logic for demo purposes
            $this->$_test = TRUE;
        else
            $this->$_test = FALSE;
    }

    public function getIt()
    {
        if ($this->$_test) { //if test is true or false do stuff/other stuff
            //do stuff
        } else {
            //do other stuff
        }
    }
}
$myObj = new Info(); //initialise the object

?>
<?php echo $myObj->getIt(); ?> //surrounded by html in my original code. Here I am getting some 'stuff' or 'other stuff' depending on what setIt() returned to $_test.

Sorry if that is a bit convoluted.

Your accessing your property with a $ at the beginning of it. When accessing object properties in an object scope, you dont need to use $. However, you've declared $_test as static, so you would set / get them using self::$_test

class Info
{
    private static $_test;

    public function __construct()
    {
        $this->setIt(); 
    }

    public function setIt()
    {
        if ("test" == "something"){
            self::$test = TRUE;
        } else {
            self::$test = FALSE;
    }

    public function getIt()
    {
        if (self::$_test) { 
            //do stuff
        } else {
            //do other stuff
        }
    }
}
$myObj = new Info(); //initialise the object

echo $myObj->getIt();

replace

$this->$_test = TRUE;

with

$this->_test = TRUE;

every where in you code.

While using $this then $ is not used with property name

This thread can be useful.

You defined $_test as static, so you should access it as:

self::$_test

or

static::$_test

Still, getters and setters should be meaningful, and be named after the field they are wrapping [i.e. getTest(), setTest($value)], even if you are just providing an example case.

If you are new to OOP, you'll be going to some troubles with understanding the difference between instance and static properties and methods.

Let's pretend to have a Circle class, which holds center and radius:

class Circle
{

  public static PI = 3.141592653589793;

  private $x;
  private $y;
  private $radius;

  public function getX(){ return $this -> x;}
  public function setX($x){ $this -> x = $x;}

  public function getY(){ return $this -> y;}  
  public function setY($y){ $this -> y = $y;}

  public function getRadius(){ return $this -> radius;}  
  public function setRadius($radius){ $this -> radius = $radius;}  

  public function __construct($x, $y, $radius)
  {

    $this -> setX($x);
    $this -> setY($y);
    $this -> setRadius($radius);

  }

  public function getArea()
  {

    return $this -> radius * $this -> radius * self::PI;

  }

  public function getLength()
  {

    return 2 * self::PI * $this -> radius;

  }

}

x, y and radius are different for each circle [instance of the class] you build: they are instance variables:

$firstCircle = new Circle(0,0,10);
$secondCircle = new Circle(5,5,2);
...

On the other hand, PI is static because it belongs to the class itself: for every circle it holds that the length/diameter ratio is 3.1415...

You'll find out that static properties and methods have wider use in OOP, but that's enough in order to have a first glance into it.