PHP Object-oriented various concepts and usage example code detailed

Source: Internet
Author: User
This article mainly introduced the PHP object-oriented programming method, combined with the case form in detail the PHP object-oriented programming involved in the concept of classes, definitions, constructors, destructors, inheritance, overloading, interfaces, abstract classes and other concepts and use of skills, the need for friends can refer to the next

This paper analyzes the object-oriented programming method of PHP. Share to everyone for your reference, as follows:

PHP5 begins to support object-oriented, as shown in the following example:

<?phpclass classname{var $attr 1;  var $attr 2;  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 protected function<br/> ';  }//Disallow inheritance of final function Oper5 () {} function get ($name) {return $this, $name;  } function set ($name, $value) {$this, $name = $value;  }//static method, statically 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/><br/> '; Echo ' Per-class constant classname::P i-'. ClassName::P i;echo ' <br/> static method: ClassName::d ouble (3)-'. ClassName::d OubLe (3); Echo ' <br/> ';//implement Inherit echo ' implementation inheritance <br/> '; class B extends classname{function oper4 () {$this->oper3 ( );  The protected method can only be used in the} function Oper1 () {//Overload echo ' This is class B/' s oper1. <br/> ';  }} $d = new B ("forth"); $d->oper1 (); $d->oper4 ();//Interface interface displayable{function display (); function Show ();}  Class C implements displayable{function display () {echo ' This is the method of the corresponding interface. <br/> '; The function Show () {}} $e = new C (); $e->display (); Echo ' Checks if $e is an instance of C: '; Echo ($e instanceof C)?  ' Yes ': ' No ';//Clone the object $f = Clone $e; Echo ' <br/><br/> can use the Clone () method, which is invoked when using the Clone keyword;//abstract class Abstraction classes e{}//$f = new E ();  This will be an error, cannot instantiate abstract class//parameter overloading, polymorphic 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 parameter $p, which is passed in as an array, must use the 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 properties of the instance foreach ($g as $att) {echo ' <br/> '. $att;} Reflect Echo ' <br/> '; $class = new Reflectionclass (' F '); Echo ' <pre> '; echo $class; Echo ' </pre> ';? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.