PHP oriented object base (interface, Class)

Source: Internet
Author: User

Introduction to PHP Object-oriented basics

1. Definition of the interface interface, class definition class, class supports abstract and final modifiers, abstract class, abstract class

Direct instantiation is not supported, and the final decorated class/method cannot be overridden by an inheritance/method.
2. Implementation of the interface through implements, class inheritance extends

 interface   ishape{ function   Draw_core ();  };  class  pathshape implements   ishape{ public  function   Draw_core () {}}  class  Rectangle extends   pathshape{ public  function   Draw_core () { // overide draw_core   

3. Static variables and constants (static, const)
A. Constant declaration variable name does not need to be preceded by dollar modifier $, static variable requires
B. Both are accessed through the class, and the static variable method needs to be added $ $ to the variable name before the Fu Hao

   class myclass{     const  m_const_value;      Static $M _static_value ;   }   MyClass::m_const_value;   MyClass::$M _static_value;

C. The constant declaration does not support the access permission modifier, and cannot be public before const, the default is public.

   Const M_const  //OKpublic   CONST//  Throw exception

4. Class-Internal access to non-static/constant variables and methods through $this, access to the parent class through parent, access to static variables and methods within the class can be
Self,self essence is a pointer to the class that can also be accessed by static

   // Parent class Method   $this // Method Instance method   Self::$static _value ; // accessing static variables   static::$static _value; // Ibid .


The difference between 5.static and self is that self refers to the parsing context and is also the function of the current class, and static refers to the call
Class instead of the containing class, the typical example is a singleton

 abstract  class   parentclass{ public  static  Span style= "color: #0000ff;" >function   CreateInstance () { return  new  static  ();  //  Self is not used here. Because self is in fact pointing to ParentClass's//If you use self, then an exception is thrown, suggesting that the abstract class cannot be instantiated//and that static does not point directly to ParentClass but to the containing class// }}  class  childclass extends  Span style= "color: #000000;" > parentclass{ //} 

7. Use interceptors in class, PHP interceptors have __get,__set,__inset,__unset,__call, only focus on geth and set interceptors

__get ($propertywhen an undefined property is accessed, the method is called __set ($property,$valuecalled when a value is assigned to an undefined propertyclassmyclass{ Public function__get ($property){       Echo"Access __get"; if(Property_exists ($this,$property)){          return $this-$property; }Else{         return"Unknown"; }    }     Public function__set ($property,$value){      if(!property_exists ($this,$property)){       $this->name =$value;//assign a value directly to the $name if the variable does not exist      }    }         Public $Name= "Visonme"; }; //Access $obj=NewMyClass ();$obj->name;//direct access to variable $name $obj->password;//password not defined, first access __get final output unknown//-for __set $obj->password = ' Fz-visonme ';//password does not exist, then will go __setz finally give $name assigned value Echo $obj->name;//Output:fz-visonme

8. Class constructors and Destructors: __construct, __destruct, constructors instantiate objects when invoked, many for member variable initialization work, destructors are called when class is destroyed, and more for finishing work

class myclass{  function  __construct () {}  function  __destruct () {}}

9. Object replication uses the Clone,clone keyword to produce a new object by using the "value Copy" method, which is copied for the object copy itself or the reference copy.

A. Simple Type assignment

class myclass{  public$ID;}; $a New MyClass; $a->id = 199; $b Clone $a ;   Echo $b->id;   // output:199

B. Replication that contains objects

classaccount{ Public $RMB;};classmyclass{ Public $ID;  Public $ACCOUNTOBJ;  Public function__construct ($c){    $this->accountobj =$c; }};$a=NewMyClass (NewAccount ());$a->accountobj->rmb= 199;$b=Clone $a;Echo $b->accountobj->rmb;//output:199$a->ACCOUNTOBJ->RMB = 100;Echo $b->accountobj->rmb;//output:100after clone,$a accountobj Change the time, which can also affect the$b

The result is obviously not what we expected, and what we expect is that AB is a separate object that does not have any associations.

To solve this problem, I can implement __clone within the class, and when we call clone outside it calls the __clonef method of the class, so we can override the __clone to achieve the control of clone. For example, the transformation of the B example

classmyclass{ Public $ID;  Public $ACCOUNTOBJ;  Public function__construct ($c){    $this->accountobj =$c; }  //__clone Implementation of clone Control//In this case, the internal clone of the account at the same time, this can be avoided in the B example of the problem   Public function__clone () {$this->id = 0;//set the ID to 0 if you need it.    $this->accountobj =Clone $this-Accountobj; }};

On the __clone method we need to know that this method is called on the object after being clone, not on the original object, for example in the B example

$b = Clone $a; Process performed: Basic Replication Object $ A---> $b execution __clone ()

PHP oriented object base (interface, Class)

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.