I always understood that it was a test error that the constructor method was automatically called after the object was created.
Class o{Public Function __construct () {var_dump ($object);}} $object = new O;
Error message: notice:undefined variable:object
Reply to discussion (solution)
constructor method to pass in a variable
Class o{
Public function __construct ($object) {
Var_dump ($object);
}
}
$str = "";
$object = new O ($STR);
Class o{
Public Function __construct () {
Var_dump ($object);
}
}
There are no $object variables in the class, so the output notice:undefined Variable:object
If you want to get $object when initializing, you can pass in, for example:
name = ' Fdipzone '; $object = new O ($oo);? >
The error is because the variable does not exist, your code does not conform to the scope of the variable convention
Plus global $object; You can do it.
You can see that the output is NULL
This is the new O before the object is assigned to the $object, and the constructor is executed when the object is created.
Class o{public function __construct () { global $object;//Plus this var_dump ($object);//Output NULL
The answer from ancient times is upstairs
The object is automatically called after @xuzuning new O, and then the object's reference is assigned to the $object. Moderator Daniel
New O is an action that creates an object from class O
PHP calls the constructor when it creates the object (so you have a chance to do something of your own)
Attention:
$object = new O;
What $object get is not a reference to new O, but the new O itself
Only one point, either PHP4 or PHP5, is the same.
@xuzuning Oh, I know how to construct a method, but I don't know when to invoke it, but I have an object on the new O and then we call the construction method and then assign the object's reference to the $object.
@xuzuning, is that right? How is the assignment of resources and objects on PHP a reference?
New is not a value assignment
It's a bundle of objects and variables.
@xuzuning, so what's the quote? Excuse me.
Class T {public $v = 1; function See () { echo $this->v, Php_eol; }} $a = new T; $b = new T; $a->v = 2; $a->see (); 2$b->see (); 1 if new passes a reference, why is it 1 instead of 2? $c = $b; Object assignment is the reference $c->v =: $b->see (); 100$c->see (); 100 so $b->v changed, $c->v changed.
@xuzuning understand.
It seems that you do not understand the construction method in the class, and so on, what is the function of the construction method, the constructor is to initialize the object, if the new object is not assigned value, the construction method is meaningless