Examples of PHP object-oriented programming methods and js object-oriented programming examples
This article analyzes the PHP object-oriented programming method. We will share this with you for your reference. The details are as follows:
PHP5 began to support object-oriented, for example:
<? Phpclass classname {var $ attr1; var $ attr2; public $ attribute; const PI = 3.14; // constructor function _ construct ($ param = 'default ') {echo "Constructor called with parameter $ param <br/>";} // destructor function _ destruct () {echo '<br/> destruct ';} // function oper1 () {echo 'oper1 <br/> ';} function oper2 ($ param) {$ this-> attr1 = $ param; echo $ this-> attr1;} protected function oper3 () {echo 'this is prote Cted function <br/> ';} // 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 '<br/>'; echo $ c-> attr1; echo '<br/> '; echo 'per-Class constant classname: PI -'. classname: PI; echo '<br/> static method: classname: double (3 )-'. classname: double (3); echo '<br/>'; // implement inherited echo 'implement inherited <br/>'; 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. <br/> ';}}$ d = new B ("forth "); $ d-> oper1 (); $ d-> oper4 (); // interface Displayable {function display (); function show ();} class C impleme CNT Displayable {function display () {echo 'This is the method of the corresponding interface. <br/> ';} function show () {}}$ e = new C (); $ e-> display (); echo 'check if $ e is an instance of C: '; echo ($ e instanceof C )? 'Yes': 'no'; // clone object $ f = clone $ e; echo '<br/> you can use the _ clone () method, when using the clone keyword, call '; // abstract class E {}/$ f = new E (); // This row returns an error and cannot instantiate the abstract class // parameter overload, polymorphism class F {public $ a = 1; public $ B = 2; public $ c = 3; function displayString ($ elem) {echo '<br/> string :'. $ elem;} function displayInt ($ elem) {echo '<br/> int :'. $ elem;} // note that the $ p parameter is passed as an array. You must use 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, read all attributes of the Instance foreach ($ g as $ att) {echo '<br/> '. $ att;} // reflect echo '<br/>'; $ class = new ReflectionClass ('F'); echo '<pre>'; echo $ class; echo '</pre>';?>