- If you define __construct in a subclass, you do not call the parent class's __construct, and if you need to call the parent class's constructor at the same time, you need to use the parent::__construct () explicit call.
1 classCar {2 function __construct () {3Print"Parent class constructor is called \ n";4 }5 }6 classTruck extends Car {7 function __construct () {8Print"The subclass constructor is called \ n";9 parent::__construct ();Ten } One } A$car =NewTruck ();
- Inside the class: Call property itself property $this->name and static self :: $name
- If the constructor is defined as a private method, it is not allowed to instantiate the object directly, which is typically instantiated by a static method, which is often used in design mode to control the creation of objects, such as a singleton pattern that only allows a globally unique object.
1 classCar {2 Private function__construct () {3 Echo' Object create ';4 }5 6 Private Static $_object=NULL;7 Public Static functiongetinstance () {8 if(Empty(Self::$_object)) {9Self::$_object=NewCar ();//internal methods can call private methods, so you can create objects hereTen } One returnSelf::$_object; A } - } - //$car = new Car ();//Do not allow direct instantiation of objects here the $car= Car::getinstance ();//using static methods to obtain an instance
- Overload
- The overloads of a property are implemented by __set,__get,__isset,__unset to assign, read, and determine whether a property is set or destroyed, respectively, for a nonexistent property.
- The overloads of the method are implemented by __calL, and when a non-existent method is called, the __call method is called, and the __callstatic overload is used when a static method that does not exist is called.
- Serialization of objects
- objects can be serialized as strings by the Serialize method, used to store or pass data, and then deserialized into objects using unserialize when needed.
1 classCar {2 Public $name= ' car ';3 }4 $a=NewCar ();5 $str=Serialize($a);//object is serialized into a string6 Echo $str.‘ <br> ';7 $b=unserialize($str);//Deserializing Objects8 Var_dump($b);
- What's the difference between a single quote and a double quote?
-
PHP allows us to include it directly in a double quote string 字串变量
.
The contents of a single quote string are always considered ordinary characters.
Like what:
$str = ' Hello '; echo "str is $STR"; Operation Result: STR is Helloecho ' str is $str '; Operation Result: STR is $STR
PHP Trivial Learning