20191125PHP abstract classes, interfaces, and magic methods

Abstract class

It can not be instantiated, for other classes inherit. Use abstract (abstract). Abstract methods must be abstract class, abstract class may not have abstract methods.

Interface interface is a special abstract class.

eg:

? < PHP
 // abstract class 
abstract  class Person {
  function RUN () {
   echo "Runing with .................." ; 
 } 
 abstract  function Start (); 
} 

class Boys the extends Person {
     function Start () {
         echo "subclass method Start " ; 
    } 
} 

$ a = new new Boys ();
 $ a -> RUN (); 

// write interface 

interface Student {
     function name ();
     function Study (); 
}

class stu implements student{
    function name(){
        echo "姓名";
    }
    function study(){
        echo "学习";
    }
}

$b=new stu();
$b->study();
==================================================================

Magic cloned __clone ().

<?php
//对象克隆和魔术方法__clone
class person{
    var $name;
    var $age;
    var $count=0;
 function __construct($name,$age){
     $this->name=$name;
     $this->age=$age;
 }
 function __clone(){
     $this->count+=1;
 }
 function run(){
  echo $this->name."runing………………";
 }
}
$ A = new new Person ( "Bob", 23 is );
 $ A -> RUN ();
 echo  $ A -> COUNT ;
 $ B = clone  $ A ;   // clone 
$ B -> RUN ();
 echo  $ B -> COUNT ;
 $ C = new new Person ( "flower", 22 is );
 echo  $ C -> COUNT ;
 $ D = clone  $ B ;
 echo  $ D -> COUNT ;

 

Magic method __get () __set ()

<?php
class person{
    private $name;
    private $age;
    function __set($name,$value){
        $this->$name=$value;
        
    }
    function __get($name){
     return $this->$name;
    }
    
}

$a=new person();
$a->name="小明";
echo $a->name; 

Magic method __call (), __ tostring ()

? < PHP
 // __call (), __ toString () 
class the Person {
     function __call ( $ name , $ v ) {
         echo "method you call." $ Name "does not exist." ; 
    } 
    Function __toString () {
       return "I person class information " ; 
    } 
} 
$ a = new new person ();
 $ a -> RUN ();
 $ a -> STOP ();
 echo  $ a ;

 Other methods of magic:

__construct (), the class constructor
__destruct (), destructor class
__call (), called when calling a non-access method in a subject
__callStatic (), called when calling a non-access method by a static mode
__get (), Called when obtaining member variable of a class
called when __set (), set the member variables of a class
__isset (), Called when calling isset () on inaccessible properties or empty ()
__unset (), when to inaccessible property called unset It is called when the ().
__sleep (), the implementation of serialize (), the first will call this function
__wakeup (), the implementation unserialize (), the first will call this function
__toString (), the class is as a response method when a string
__invoke (), the calling function way to respond to call an object method when
__set_state (), call var_export () when the derived class, this static method will be called.
__clone (), invoked when the object copy is complete
__autoload (), attempts to load undefined class
__debugInfo (), print debug information needed

 

 

Guess you like

Origin www.cnblogs.com/syqlwyx/p/11926753.html