Some functions in PHP object-oriented programming

Source: Internet
Author: User

    1. Get_object_vars function to convert a field with access in an object to an array

<?phpclass foo {private $a;    public $b = 1;    Public $c;    Private $d;       Static $e;    Public Function test () {Var_dump (Get_object_vars ($this)); }} $test = new Foo;var_dump (Get_object_vars ($test)); $test->test ();? >

Results

Array (2) {["B"]=> int (1) ["C"]=> Null}array (4) {["a"]=> null ["B"]=> int (1) ["C"]=> NULL ["D"] = NULL}

2.array get_class_methods (mixed $class _name) with array get_class_vars (string $class _name)

Get all the accessible methods in the class

<?phpclass myclass {    // constructor    public   function myclass ()     {         Return (true);    }    // method 1     PROTECTED&NBSP;FUNCTION&NBSP;MYFUNC1 ()     {         return (True);    }    // method 2    &NBSP;PRIVATE&NBSP;FUNCTION&NBSP;MYFUNC2 ()     {         return (True);    }        public  Function description ()     {                  $class _methods = get_class_methods ($this);         //or         $class _methods = get_class_methods (__CLASS__);         foreach  ($class _methods as  $method _name)  {             echo  "$method _name\n";         }    }} $class _methods = get_class_methods (' MyClass ');  //get all public method names foreach  ($class _methods as  $method _name)  {     echo  "$method _name\n";} The result is = = "Myclass description$class_methods = get_class_methods (New myclass ());//Get all the public method names , note that the construction method is private or protected times wrong foreach  ($class _methods as  $method _name)  {     echo  "$method _name\n";} The result is = = "Myclass description$t = new myclass (); $t->description ();//The result is = =" myclass  Myfunc1 myfunc2 description?>

2.__call Magic Method

Called when a non-existent method is called in the official PHP description, the definition is not complete and the correct definition is to invoke a method that does not exist or is not public.

<?phpclass User {//Method 1 protected function say () {return (true);    }//Method 2 Private Function sing () {return (true);  Public Function __call ($name, $args) {$t = Func_get_args ();    The obvious print_r ($t) are always used in the default function. }} $m = new User (); $m->say (); does not trigger $m->sing ();//Trigger $m->talk ();//Trigger?>

2.__set,__get,__unset,__isset

Sometimes objects in order to adapt to changes, such as the model of the field is often changed, need to change the field when zero, you need to dynamically add or delete properties

<?phpclass propertytest {     //Dynamic Data storage container     private   $data  = array ();      /**   overloads cannot be used on properties that have already been defined   * /    public  $declared  = 1;     /**   Overloading occurs only when this property is accessed from outside the class  */    private  $hidden  = 2;     public function __set ($name,  $value)      {        //  $this $name  =  $value;   //This is also a way of dynamic expansion, but it's more cumbersome to manage           $this->data[$name] =  $value;     }     public function __get ($name)      {         echo  ' getting  ' $name ' \ n ';         if  (Array_key_exiSTS ($name,  $this->data)  {             return  $this->data[$name];        }          $trace  = debug_backtrace ();         trigger_error (             ' undefined  Property via __get ():  '  .  $name  .              '  in  '  .  $trace [0][' file '] .              '  on line  '  .  $trace [0][' line '],             e_user_notice);         return null;    }    /**   php 5.1.0 later version, call, when using  isset judgmentCall  */    public function __isset ($name)      {         echo  "is  ' $name '  set?\n";         return isset ($this->data[$name]);    }     /**  php 5.1.0 later, when a property is deleted  */    public function  __unset ($name)      {        echo  " unsetting  ' $name ' \ n ';         unset ($this->data[$name]);     }    /**   Non-Magic method   */    public  function gethidden ()      {         return  $this->hidden;    }}echo  "<pre>\n"; $obj  = new  PropertyTest, $obj-&GT;A&NBSP;=&NBsp;1;echo  $obj->a .  "\ n", Var_dump (Isset ($obj->a)), unset ($obj->a); Var_dump (Isset ($ obj->a);echo  "\ n";echo  $obj->declared .  "\ n";echo  $obj->gethidden ()   .  "\ n";echo  $obj->hidden .  "\ n";? >

The following fragment in the code, similar to the log Trace tool

$trace = Debug_backtrace ();            Trigger_error (' Undefined property via __get (): '. $name. ' In '.            $trace [0][' file ']. ' On line '. $trace [0][' line '], e_user_notice);

4.get_class, get_parent_class,interface_exists,is_subclass_of,is_a,instanceof

<?phpif (!interface_exists (' Man ')) {    interface man    {         public function talk ($msg);     }}class  dad {    function dad ()     {    //  implements some logic    }}class child extends dad  Implements man{       public function __tostring ()      {         return  "I ' m ". Get_parent_ Class ($this). "' S son\n ";     }     public function talk ($msg)      {        echo  $msg;      }}class child2 extends dad {    public function  __tostring ()     {        return  "I ' m ". get_ Parent_class (' child2 '). "' S son too\n ";     }} $foo  = new child (); $bar  = new  Child2 (); $dad  = new dad ();echo  ' <pre> ';  var_dump (Get_class ($dad));  var_ Dump (Get_class ($foo));  var_dump (Get_class ($bar));echo  " 

The results are as follows

String (3) "Dad" string (5) "Child" string (6) "Child2"--------------------------------------------------------------- ----bool (FALSE) string (3) "Dad" string (3) "Dad"------------------------------------------------------------------- BOOL (TRUE) bool (FALSE) bool (TRUE)-------------------------------------------------------------------bool (TRUE) BOOL (FALSE) bool (TRUE)-------------------------------------------------------------------bool (TRUE) bool (true) BOOL (TRUE)-------------------------------------------------------------------bool (TRUE) bool (TRUE) bool (true)


All right, here we go today.


Some functions in PHP object-oriented programming

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.