PHP overload __get __set isset unset __call __callstatic python static method import static import difference gcc static link static Library

Source: Internet
Author: User
Tags access properties python static method
Overloading in PHP differs from traditional object-oriented overrides, such as in Java:

class A{  public void  methodName(参数1);  public void  methodName(参数1,参数2);  public void  methodName(参数1,参数2,参数3);  ...}

Overloading in PHP is an "elegant" error-handling mechanism when a class or object calls a property or method that does not exist. I wonder why PHP is also called overloading, which is irrelevant to the overloads of traditional object-oriented programming.
PHP's overloading relies on the Magic Method __get () __set () isset () unset () __call () () __callstatic (), which automatically calls these magic methods when we access properties or methods that do not exist.

Code:


    Header"Content-type:text/html;charset=utf-8"); classSample{ Public$p 1=1;//The attribute declared in the class P1//Save data that is overloadedPrivate$data=Array();/* __get is called $name when accessing a property that does not exist: variable name */ function__get($name) {"
__get:: ".$name;if(Array_key_exists ($name,$this->data)) {return$this->data[$name]; }$trace= Debug_backtrace (); Trigger_error (' Accessing a property that does not exist in a class '.$name.' file: '.$trace[0][' file '].' line '.$trace[0][' line '], e_user_warning);returnNULL; }/ * __set is called when an attribute that does not exist is assigned a value $name: variable name $value: Variable value */ function__set($name,$value){Echo"
__set:: $name = $value ";$this->data[$name] =$value; }/ * The property that does not exist uses isset () or empty () when __isset is called $name: variable name $value: Variable value * / function__isset($name){Echo"
Isset:: $name ";returnisset($this->data[$name]); }/* The property that does not exist is called when using unset () $name: variable name $value: Variable value * / function__unset($name){Echo"
__unset:: $name ";unset($this->data[$name]); }/ * This method is called automatically when the non-existent instance method of an object of the class is called, and the method must be public */ Public function__call($name , $arguments){if($name===' F1 '){Echo"
".$name."method is called,"."Parameters:"; Var_dump ($arguments); }Elseif($name===' F2 '){Echo"
".$name."method is called,"."Parameters:"; Var_dump ($arguments); }Else{Trigger_error ("Illegal call! ", e_user_warning); } }/* When a static method call is not present for an object of the class, this method is called automatically 5.3.0 new __callstatic () Magic method. A warning is generated when visibility is not set to public or not declared as static */ PublicStatic function__callstatic($name , $arguments){Echo"
".$name."The static method is called,"."Parameters:"; Var_dump ($arguments); } }$s=NewSample ();Echo"
To access an instance property that exists in a class: S->p1:: ".$s->p1;The //property overload can only be done in the object. In static methods, these magic methods will not be called. So none of these methods can be declared static. Starting with PHP 5.3.0, defining these magic methods as static generates a warning. //fatal error:access to undeclared static Property:sample:: $p 3//echo "
Accessing a static property that does not exist in a class does not call the Magic method __get: ". Sample:: $p 3;//Error//access to properties that do not exist in the class, the Magic method __get in the class is calledEcho$s->p2;//Assign a value to a property that does not exist in the class, and the Magic method __set in the class is called$s->P2 = the;Echo'
'.$s->p2;//Output//The Magic method in the class __isset will be calledVar_dump (isset($s->P2));//The Magic method in the class __isset will be calledVar_dump (Empty($s->P2));//The Magic method in the class __unset will be calledunset($s->P2);Echo'
'.$s->p2;//P2 was destroyed, error$s->F1 ();$s->F1 (1,2);$s->F1 (1,2,"Hello");$s->F2 (true,"111");$s->F3 ();//A static method that calls an object that does not existSample::say (' Hello '); Sample::say (' Hello ',' php ');?>

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP overload __get __set isset unset __call __callstatic, including the static aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.