The examples in this article describe the object-oriented features of PHP. Share to everyone for your reference, as follows:
demo1.php
<?php header (' content-type:text/html; charset=utf-8; '); Create a Computer class computer { //what is called within a class is the scope of the curly braces that create the class inside the class, outside the class. //public is the public domain of the field, which can be accessed outside of the field class, and the assignment and the $_name = ' Lenovo '; $computer = new computer (); $computer-_name = ' Dell '; echo $computer->_name;? >
demo2.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { //private is privatized, that is, the operation that encapsulates the field, cannot be accessed outside the class, and the value and assignment cannot operate private $_name = ' Lenovo '; } $computer = new computer (); echo $computer->_name;? >
demo3.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { private $_name = ' Lenovo '; This time I use a public external method to access private fields //Because private fields can only be accessed within the class, and public methods outside are within the class. //And public methods are public, so they can be accessed outside the class. Public function _run () { //field must be a class-a field when called within a class, and $_name is just a normal variable. //Field The method called outside the class is an object---field, and the class must use computer-_name ///But in this class, you can use a keyword instead of the word instead of the computer, which is $this echo $this->_name; } } $computer = new computer (); $computer-_run ();? >
demo4.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { private $name; Private $model; Private $cpu; Private $keyboard; Private $show; Private $ZB; You must write an external entry to take the public function GetName () { return $this->name; } You must write an internal entry to assign a private field to the public function SetName ($name) {// the $name here is just a variable, parameter only //$this->name Is the field of the class $this->name = $name; } } $computer = new computer (); echo $computer->getname (); $computer->setname (' Dell '); echo $computer->getname ();? >
demo5.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { private $_name; Private $_model; Private $_cpu; When an object outside the class calls the private field directly, it will follow to check if there is an interceptor, //If the $_name is assigned directly, then the __set method will intercept and no error will be made. // using interceptors for assignment and value //assignment of private function __set ($_key,$_value) {// using $_key = ' _name ', then $_value = ' Lenovo '; //$this->_name = ' Lenovo '; $this->$_key = $_value; } Value Private Function __get ($_key) { return $this, $_key; If $_key = ' _name ' then $this and _name; If $_key = ' _cpu ' then $this and _cpu; If $_key = ' _model ' then $this and _model; } } $computer = new computer (); $computer->_name = ' Lenovo '; $computer->_cpu = ' quad core '; $computer->_model = ' i7 '; echo $computer->_name; echo $computer->_cpu; echo $computer->_model;? >
demo6.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { private $_name; Private $_model; Private $_cpu; The __set and __get methods are private, or can be executed because //because the current pointer to the program is already within the class. In a class, you can execute a private method inside or out of a class, without any errors. //It only needs to be intercepted indirectly. Interception is performed in the inner class. ///Plainly, __set () and __get () are PHP built-in methods that have a certain particularity of the private function __set ($_key, $_value) { $this->$_key = $_ value; } Private Function __get ($_key) { return $this->$_key; } } $computer = new computer (); $computer->_name = ' Lenovo '; $computer->_cpu = ' quad core '; $computer->_model = ' i7 '; echo $computer->_name; echo $computer->_cpu; echo $computer->_model;? >
demo7.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { Const NAME = ' DELL '; } Output method Class for constants:: Constant echo computer::name; Dell?>
demo8.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer {public $_count = 0; Public Function _add () { $this-_count++; $_count = $_count+1 $_count++ } } //Make a cumulative effect $computer 1 = new computer (); $computer 1->_add (); $computer 1->_add (); $computer 1->_add (); echo $computer 1-_count; echo ' <br/> '; $computer 2 = new computer (); $computer 2->_add (); $computer 2->_add (); $computer 2->_add (); echo $computer 2-_count;? >
demo9.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer {public static $_count = 0; Public Function _add () { ///If it is a static member field, it should be called with self instead of $this self::$_count++;} } Make a cumulative effect $computer 1 = new computer (); $computer 1->_add (); echo Computer::$_count; $computer 1->_add (); echo Computer::$_count; $computer 1->_add (); echo Computer::$_count; echo ' <br/> '; $computer 2 = new computer (); $computer 2->_add (); echo Computer::$_count; $computer 2->_add (); echo Computer::$_count; $computer 2->_add (); Echo computer::$_count;? >
demo10.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer {public static $_count = 0; public static function _add () { self::$_count++; } } Computer::_add (); Computer::_add (); Computer::_add (); Echo computer::$_count;? >
demo11.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { } $computer = new computer (); echo $computer instanceof computer;? >
demo12.php
<?php header (' content-type:text/html; charset=utf-8; '); This is the parent class, computer class computer {public $_name = ' Lenovo '; Public Function _run () { echo ' Lenovo is running! '; } } Subclass, Laptop class Notecomputer extends computer { } $noteComputer = new Notecomputer (); Echo $noteComputer-_name; $noteComputer-_run ();? >
demo13.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer {public $_name = ' Lenovo '; Public Function _run () { echo ' Lenovo is running! '; } } Class Notecomputer extends Computer { //I don't need the fields and methods of the parent class, you can override the fields and methods of the parent class by overriding the method public $_name = ' Dell '; Public Function _run () { echo ' Dell is running! '; } } $noteComputer = new Notecomputer (); Echo $noteComputer-_name; $noteComputer-_run ();? >
demo14.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer { //privatized, but cannot be inherited by the quilt class, this time should be used to encapsulate protected $_name = ' Lenovo ' with protected modifiers; protected function _run () { return ' Lenovo is running! '; } } Class Notecomputer extends Computer {public function GetTop () { echo $this->_name; echo $this->_run (); } } $noteComputer = new Notecomputer (); $noteComputer-GetTop ();? >
demo15.php
<?php header (' content-type:text/html; charset=utf-8; '); Class Computer {public $_name = ' Lenovo '; Public Function _run () { return ' Lenovo is running! '; } } Class Notecomputer extends Computer { //My subclass has overridden the fields and methods of the parent class, //But I want to call the parent class's fields and methods again, what do I do? Public $_name = ' Dell '; Public Function _run () { echo ' Dell is running! '; Echo Parent:: _run (); } } $noteComputer = new Notecomputer (); Echo $noteComputer-_name; $noteComputer-_run (); Delldell is running! Lenovo is running!? >
demo16.php
<?php header (' content-type:text/html; charset=utf-8; '); Final if added in front of the class, indicates that the class cannot be inherited//final class Computer {//} class Computer { //final if added in front of the method, indicating that the method cannot be overridden Final public Function _run () { } } class Notecomputer extends computer {public function _run () { } } $noteComputer = new Notecomputer ();? >
demo17.php
<?php header (' content-type:text/html; charset=utf-8; '); Create an abstract class, so long as the abstract class is preceded by class, or the abstract class can not be instantiated, that is, to create an object//only in the class there is an abstract method, then this class must be an abstract class, the class must be preceded by abstract abstract class Computer {public $_name = ' Lenovo '; Abstract class creates an abstract method //abstract method that does not implement the content of the method body abstract public Function _run (); Can I create a common method in an abstract class public function _run2 () { echo ' I am the normal method of the parent class '; } } Class is not able to implement multiple inheritance, only single inheritance is supported. //Abstract class is used to inherit the subclass, implement a specification and resource sharing class Notecomputer extends Computer { //abstract class of abstract method, subclasses must rewrite, or will error. the ordinary method in the abstract class does not need to be rewritten, and the subclass inherits directly from the public function _run () { echo ' I am the method of the subclass '; } } $noteComputer = new Notecomputer (); $noteComputer-_run (); $noteComputer-_run2 (); Echo $noteComputer-_name;? >
demo18.php
<?php / * Should I use an abstract class or an interface? * If you want to inherit the method specification of multiple interfaces, then use the interface well. * If you want to share a method body content, then use abstract class. * * * header (' content-type:text/html; charset=utf-8; '); Create an interface //interface can not be instantiated //interface is to standardize the implementation of its subclasses, in order to achieve a unified purpose. You can also share data interface The computer { ///member field must be a variable const NAME = ' member '; All of the methods in the interface are abstract methods, not able to write the method body//and the interface abstract method does not need to write the abstract public function _run (); Public function _run2 (); } Interface Computer2 {public function _run3 (); } The subclass inherits the interface, called the implementation, the interface can implement class Notecomputer implements Computer,computer2 {public function _run () { Echo ' I rewrote run '; } Public Function _run3 () { echo ' I rewrote run3 '; } Public Function _run2 () { echo ' I rewrote run2 '; } } $noteComputer = new Notecomputer (); $noteComputer-_run (); $noteComputer-_run2 (); $noteComputer-_run3 (); echo notecomputer::name; Interface:: Constant //echo computer::name;? >
demo19.php
<?php header (' content-type:text/html; charset=utf-8; '); What is called polymorphic, literal meaning, a variety of forms//an action by different people to execute, and produce different effects or effects, that is polymorphic. A person performs the same action in a different state, forming different effects, or as polymorphic. Gardener Shears Repair flowers//Barber cut haircut//Total cut layoff//person notebook running win7 boot up//People desktop running XP boot up//Create an interface to standardize the way of running Inter Face Computer {public Function version ();//This method indicates what computer public function work () is used; How does this computer work}//create a notebook class implementation interface class Notecomputer implements computer {public Function version () {echo ' notebook '; } public Function work () {echo ' can run Win7 ' in portable mode; }}//Creating a Desktop class implementation interface class Desktopcomputer implements computer {public Function version () {echo ' desktop '; } public Function work () {echo ' runs XP on the workstation '; }}//Create a user class Person {//Create a method to accept a computer (laptop or desktop computer)///How to accept it, and send their object in on the OK. Public Function _run ($type) {echo ' this person's '; $type, version (); $type->work (); }}//The principle of polymorphism is that the class is written, do not modify it, as long as the call parameter outside the class changes//And the final result will be changed, then this is polymorphic. YesAn interface, two classes, one is a notebook class, one is the desktop class//created notebook $noteComputer = new Notecomputer (); Create Desktop $desktopComputer = new Desktopcomputer (); Create a man $person = new Person (); Using computer $person-_run ($noteComputer); This transfer, called the transitive?> of the object reference
I hope this article is helpful to you in PHP programming.