Class UserName
- {
- Defining properties
- Private $name;
- Defining constructors
- function __construct ($name) {
- $this->name = $name; The this pointer is already used here
- }
Destructors
- function __destruct () {}
- Print User name member function
- function Printname () {
- Print ($this->name); The this pointer is also used
- }
- }
Instantiating an Object
- $nameObject = new UserName ("Heiyeluren");
- Perform printing
- $nameObject->printname (); Output: Heiyeluren
- Second instantiation of the object
- $nameObject 2 = new UserName ("PHP5");
- Perform printing
- $nameObject 2->printname (); Output: PHP5
- ?>
Copy CodeThe above class uses the this pointer in 11 rows and 20 rows respectively, so who is this at that point? In fact this is at the time of instantiation to determine who to point to, such as when the first instantiation of the object (25 rows), then this is pointing to the $nameobject object, then the execution of 18 lines of printing when the print ($this , name), Then of course the output is "Heiyeluren". In the second instance, print ($this->name) becomes print ($nameObject 2->name) and the "PHP5" is output. So, this is a pointer to the current object instance and does not point to any other object or class. (2) Self first, to be clear, is to point to the class itself, that is, that it does not point to any object that has already been instantiated, and that is typically used to point to a static variable in the class.
Class Counter
- {
- Defining attributes, including a static variable
- private static $firstCount = 0;
- Private $lastCount;
constructor function
- function __construct () {
- $this->lastcount = ++selft:: $firstCount; Use self to invoke a static variable, using the self call must use::(domain operator symbol)
- }
Print the maximum number of times
- function Printlastcount () {
- Print ($this->lastcount);
- }
- }
Instantiating an Object
- $countObject = new Counter ();
- $countObject->printlastcount (); Output 1
- ?>
Copy CodeNote Two places: lines 6th and 12th. A static variable $firstcount is defined in the second row, and the initial value is 0, then the value is called at 12 rows, using self to invoke, and the middle uses "::" To connect, that is, the so-called domain operator, then called the class itself defined static variable $ Frestcount, a static variable is not related to an instance of the following object, it's just about the class, and I can't use this to refer to the class itself, which can be referenced by self, because self is pointing to the class itself, regardless of any object instance. In other words, if you want to use a static member inside a class, you must also use self to invoke it. (3), parent we know that parent is a pointer to the parent class, and the parent is generally used to invoke the constructor of the parental class.
Base class
- Class Animal
- {
- Properties of the base class
- Public $name; Name
- Constructors for base classes
- Public function __construct ($name) {
- $this->name = $name;
- }
- }
Derived classes
- Class Person extends Animal//person classes inherit the Animal class
- {
- Public $personSex; Gender
- Public $personAge; Age
- Constructors for inheriting classes
- function __construct ($personSex, $personAge) {
- Parent::__construct ("Heiyeluren"); The constructor for the parent class was called with parent
- $this->personsex = $personSex;
- $this->personage = $personAge;
- }
- function Printperson () {
- Print ($this->name. ' is '. $this->personsex. ", this year". $this->personage);
- }
- }
Instantiating a Person object
- $personObject = new Person ("male", "21");
- Perform printing
- $personObject->printperson (); Output: Heiyeluren is male,this year 21
- ?>
Copy CodeNote The details: The member properties are public, especially the parent class, for the inheriting class to access through this. Note the key: the 25th line: Parent: __construct ("Heiyeluren"), when we use the parent to invoke the constructor of the parents to initialize the parent class, because the members of the parent class are public. We are then able to invoke this directly in the inheriting class using this. |