1. Static Properties and methods
Each instance of a class has its own properties and methods, each class can also contain static properties, static properties are not part of any instance of the class, and static properties can be understood as global variables stored in the class, and static properties can be referenced anywhere through the class name.
1<?PHP2 classMyClass {3 Static $a= 0; 4 functionGetValue () {5 returnSelf::$a;6 }7 }8 9 $obj=NewMyClass ();Ten Echo $obj->getvalue ().Php_eol; One A EchoMyClass::$a.Php_eol; -?>
2, the application of static properties-----Pass a unique ID to all instances of the class
<?PHPclassMyClass {Static $count= 0; Public $unique _id; function__construct () { self::$count++; $this->unique_id = self::$count; } } $obj 1=NewMyClass (); Echo $obj 1->unique_id.Php_eol; $obj 2=NewMyClass (); Echo $obj 2->unique_id.Php_eol;?>
3, static method
1<?PHP2 classHelloWorld {3 Static functionSayHello () {4 Print"Hello World";5Self::printnewline ();6 }7 8 Static functionPrintnewline () {9 Echo Php_eol;Ten } One } AHelloWorld::SayHello (); -HelloWorld::SayHello (); -?>
4, constants of the class
> Global constants defined with the Define function
The constants of the > class are similar to static members, and they belong to the class itself, not to instances of the class
The constants of the > class are case-sensitive
<?PHPclassMycolorenumclass {ConstRed = "Red"; ConstGreen = "Green"; ConstBlue = "Blue"; Static functionPrintcolor () {PrintSelf::red.Php_eol; PrintSelf::green.Php_eol; PrintSelf::blue.Php_eol; }} Mycolorenumclass::Printcolor ();?>
5, Object cloning
In PhP5, the object assignment is actually a reference, if you need to copy the object, clone with the cloned keyword
<?PHPclassMyClass { Public $var= 1; } $obj 1=NewMyClass (); //$obj 2 = $obj 1; $obj 2=Clone $obj 1; $obj 2-var= 2; //using $obj2 = $obj 1, output below 2//using $OBJ2 = Clone $obj 1, output 1 below Echo $obj 1-var.Php_eol;?>
6, polymorphic
In this example, if you want to add another animal, you need to change 2, you need to add animal classes and methods, and you need to add a ElseIf branch to the Printtherightsound method, which is very unfriendly to code maintenance and separation.
<?PHPclasscat{functionMiao () {Echo"Cat"; } } classdog{functionWang () {Echo"Dog"; } } functionPrinttherightsound ($obj ){ if($objinstanceof Cat) { $obj-Miao (); }Else if($objinstanceof Dog) { $obj-Wang (); }Else { Echo"The wrong object type was passed"; } Echo Php_eol; } printtherightsound (NewCat ()); Printtherightsound (NewDog ());?>
We can use the advantages of inheritance to transform the above shortcomings
1<?PHP2 Abstract classAnimal {3 Abstract functionMakeSound ();4 }5 classCatextendsanimal{6 functionMakeSound () {7 Echo"Cat";8 }9 }Ten classDogextendsanimal{ One functionMakeSound () { A Echo"Dog"; - } - } the classChickenextendsAnimal { - functionMakeSound () { - Echo"Chicken"; - } + } - + functionPrinttherightsound ($obj ){ A if($objinstanceof Animal) { at $obj-MakeSound (); -}Else { - Echo"The wrong object type was passed"; - } - Echo Php_eol; - } in -Printtherightsound (NewCat ()); toPrinttherightsound (NewDog ()); +Printtherightsound (NewChicken ()); -?>
After this transformation, Printtherightsound will not need to change, only need to add the corresponding animal classes and methods can
PHP Object-oriented Essentials (1)