We generally declare a class first, and then use this class to instantiate the object!
However, when we declare this class, we want to use the attributes or methods of this class in the class itself. What should I do?
For example:
I declare a user class! It only contains one attribute $ name;
Class user
{
Public $ _ name;
}
Now, I add a method to the user class. Use the getname () method to output the value of the $ name attribute! Copy the PHP content to the clipboard.
PHP Code:
Copy codeThe Code is as follows: class user
{
Public $ name;
Function getname ()
{
Echo $ this-> name;
}
}
// How to use it?
$ User1 = new user ();
$ User1-> name = 'zhangsan ';
$ User1-> getname (); // three items are output here!
$ User2 = new user ();
$ User2-> name = 'Li si ';
$ User2-> getname (); // Li Si will be output here!
How can this problem be solved?
I created two user objects above. $ User1 and $ user2 respectively.
When I call $ user1-> getname. The above user class code echo $ this-> name; is equivalent to echo $ user1-> name;
This is probably the meaning!