This article mainly introduces the object-oriented features of the PHP getting started tutorial, and analyzes the inheritance, polymorphism, interfaces, abstract classes and abstract methods involved in php object-oriented in the form of examples, for more information about PHP object-oriented features, see the example in this article. We will share this with you for your reference. The details are as follows:
Demo1.php
_ Name = 'Dell '; echo $ computer-> _ name;?>
Demo2.php
_ Name;?>
Demo3.php
$ _ Name is just a common variable. // The method for calling a field outside the class is the object-> Field, and the Computer-> _ name/must be used in the class, but in this class, you can use a keyword instead of a word to replace the Computer, that is, $ this echo $ this-> _ name ;}$ computer = new Computer (); $ computer-> _ run ();?>
Demo4.php
Name ;}// an internal entry must be written, and the private field is assigned a public function setName ($ name) {// The $ name here is just a variable, parameter only // $ this-> name is the class field $ this-> name = $ name ;}$ computer = new Computer (); echo $ computer-> getName (); $ computer-> setName ('Dell '); echo $ computer-> getName ();?>
Demo5.php
_ Name = 'Lenovo '; $ this-> $ _ key = $ _ value;} // value: private function _ get ($ _ key) {return $ this-> $ _ key; // if $ _ key = '_ name', then $ this-> _ name; // if $ _ key = '_ cpu', $ this-> _ cpu; // if $ _ key = '_ model', $ this-> _ 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
$ _ 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
Demo8.php
_ Count ++; // $ _ count = $ _ count + 1 $ _ count ++} // perform a cumulative effect $ computer1 = new Computer (); $ computer1-> _ add (); $ computer1-> _ add (); $ computer1-> _ add (); echo $ computer1-> _ count; echo'
'; $ Computer2 = new Computer (); $ computer2-> _ add (); $ computer2-> _ add (); $ computer2-> _ add (); echo $ computer2-> _ count;?>
Demo9.php
_ Add (); echo Computer: $ _ count; $ computer1-> _ add (); echo Computer: $ _ count; $ computer1-> _ add (); echo Computer: $ _ count; echo'
'; $ Computer2 = new Computer (); $ computer2-> _ add (); echo Computer: $ _ count; $ computer2-> _ add (); echo Computer :: $ _ count; $ computer2-> _ add (); echo Computer: $ _ count;?>
Demo10.php
Demo11.php
Demo12.php
_ Name; $ noteComputer-> _ run ();?>
Demo13.php
_ Name; $ noteComputer-> _ run ();?>
Demo14.php
_ Name; echo $ this-> _ run () ;}$ noteComputer = new NoteComputer (); $ noteComputer-> getTop ();?>
Demo15.php
_ Name; $ noteComputer-> _ run (); // DellDell is running! Lenovo is running!?>
Demo16.php
Demo17.php
_ Run (); $ noteComputer-> _ run2 (); echo $ noteComputer-> _ name;?>
Demo18.php
_ Run (); $ noteComputer-> _ run2 (); $ noteComputer-> _ run3 (); echo NoteComputer: NAME; // interface: constant // echo Computer:: NAME;?>
Demo19.php
Version (); $ type-> work () ;}// the principle of polymorphism, that is, the classes are all written. do not modify them, as long as the parameter changes outside the class and the final result is also changed, this is polymorphism. // There is one interface, two classes, one is the notebook class, and the other is the desktop class // The Notebook $ noteComputer = new NoteComputer () is created (); // Create a desktop $ your topcomputer = new Your topcomputer (); // create a person $ Person = new person (); // use the computer $ person-> _ run ($ noteComputer ); // This transfer is called Object reference transfer?>
I hope this article will help you with PHP programming.
For more articles on object-oriented feature analysis (inheritance, polymorphism, interfaces, abstract classes, and abstract methods) in PHP Getting Started Tutorial, refer to PHP Chinese network!