This article mainly introduces the PHP object-oriented programming method, this paper analyzes in detail the concepts, definitions, constructor, destructor, inheritance, overloading, interfaces, abstract classes, and other concepts and usage skills of classes involved in php object-oriented programming with examples, for more information about PHP object-oriented programming, see the example in this article. We will share this with you for your reference. The details are as follows:
PHP5 began to support object-oriented, for example:
";}// Destructor function _ destruct () {echo'
Destruct ';} // function oper1 () {echo 'oper1
';} Function oper2 ($ param) {$ this-> attr1 = $ param; echo $ this-> attr1;} protected function oper3 () {echo 'this is protected function
';} // Do not inherit from final function oper5 () {} function _ get ($ name) {return $ this-> $ name;} function _ set ($ name, $ value) {$ this-> $ name = $ value;} // static function double ($ param) {return $ param * $ param ;}} $ a = new classname ('first'); $ B = new classname ('second'); $ c = new classname (); $ c-> oper2 ("hello"); echo'
'; Echo $ c-> attr1; echo'
'; Echo' Per-Class constant classname: PI-'. classname: PI; echo'
Static method: classname: double (3)-'. classname: double (3); echo'
'; // Implement inherited echo' implement inherited
'; Class B extends classname {function oper4 () {$ this-> oper3 (); // The protected method can only be used in} function oper1 () {// reload echo 'this is class B/'s oper1.
';}}$ D = new B ("forth"); $ d-> oper1 (); $ d-> oper4 (); // interface Displayable {function display (); function show ();} class C implements Displayable {function display () {echo 'this is the method of the corresponding interface.
';} Function show () {}}$ e = new C (); $ e-> display (); echo' check whether $ e is a C instance :'; echo ($ e instanceof C )? 'Yes': 'no'; // clone object $ f = clone $ e; echo'
You can use the _ clone () method to call '; // abstract class E {}// $ f = new E () when using the clone keyword (); // This row reports an error. the abstract class cannot be instantiated. // The parameter is overloaded. the multi-state class F {public $ a = 1; public $ B = 2; public $ c = 3; function displayString ($ elem) {echo'
String: '. $ elem;} function displayInt ($ elem) {echo'
Int :'. $ elem;} // note that the $ p parameter is passed as an array and must be used as a subscript to access function _ call ($ method, $ p) {if ($ method = 'display') {if (is_string ($ p [0]) {$ this-> displayString ($ p [0]);} else {$ this-> displayInt ($ p [0]) ;}}$ g = new F (); $ g-> display ('ABC '); // iterator that reads all attributes of the instance foreach ($ g as $ att) {echo'
'. $ Att;} // reflection echo'
'; $ Class = new ReflectionClass ('F'); echo'';echo $class;echo '
';?>
I hope this article will help you with PHP programming.
For more details about PHP object-oriented programming method examples, refer to PHP Chinese network!