Home > Backend Development > PHP Tutorial > Overloading of php __get __set isset unset __call __callStatic python static method import static import difference gcc static link static library

Overloading of php __get __set isset unset __call __callStatic python static method import static import difference gcc static link static library

WBOY
Release: 2016-07-29 08:53:10
Original
1238 people have browsed it

overloading in php is different from traditional object-oriented rewriting, such as in java:

<code>class A{

  public void  methodName(参数<span>1</span>);
  public void  methodName(参数<span>1</span>,参数<span>2</span>);
  public void  methodName(参数<span>1</span>,参数<span>2</span>,参数<span>3</span>);
  <span>...</span>}</code>
Copy after login

Overloading in php is an "elegant" error handling mechanism when calling non-existent properties or methods on a class or object. I don’t understand why this is also called overloading in PHP. It has nothing to do with overloading in traditional object-oriented programming.
PHP overloading relies on the magic methods __get() __set() isset() unset() __call() __callStatic(). When we access non-existent properties or methods, the system will automatically call these magic methods.

Code:

<code><span><span><?php</span>
header(<span>"content-type:text/html;charset=utf-8"</span>);

<span><span>class</span><span>Sample</span>{</span><span>public</span><span>$p1</span> = <span>1</span>;<span>//类中声明的属性p1</span><span>//保存被重载的数据</span><span>private</span><span>$data</span> = <span>array</span>(); 

 <span>/*
   在访问对象不存在的属性时__get被调用
   $name:变量名
  */</span><span><span>function</span><span>__get</span><span>(<span>$name</span>)</span>
  {</span><span>"<br>__get:: "</span>.<span>$name</span>;
    <span>if</span>(array_key_exists(<span>$name</span>, <span>$this</span>->data)){
        <span>return</span><span>$this</span>->data[<span>$name</span>];
    }

    <span>$trace</span> = debug_backtrace();
    trigger_error(
        <span>'访问类中不存在的属性'</span>.<span>$name</span>.
        <span>' 文件:'</span>.<span>$trace</span>[<span>0</span>][<span>'file'</span>].
        <span>' 所在行'</span>.<span>$trace</span>[<span>0</span>][<span>'line'</span>]
        ,E_USER_WARNING);
    <span>return</span><span>null</span>;

  }

  <span>/*
   对象不存在的属性赋值时__set被调用
   $name:变量名
   $value:变量值
  */</span><span><span>function</span><span>__set</span><span>(<span>$name</span>,<span>$value</span>)</span>{</span><span>echo</span><span>"<br> __set:: $name = $value "</span>;
     <span>$this</span>->data[<span>$name</span>] = <span>$value</span>;

  }

  <span>/*
   对象不存在的属性使用isset()或empty() 时__isset被调用
   $name:变量名
   $value:变量值
  */</span><span><span>function</span><span>__isset</span><span>(<span>$name</span>)</span>{</span><span>echo</span><span>"<br>isset::  $name "</span>;
    <span>return</span><span>isset</span>(<span>$this</span>->data[<span>$name</span>]);
  }


  <span>/*
   对象不存在的属性使用unset()时被调用
   $name:变量名
   $value:变量值
  */</span><span><span>function</span><span>__unset</span><span>(<span>$name</span>)</span>{</span><span>echo</span><span>"<br>__unset::  $name"</span>;
    <span>unset</span>(<span>$this</span>->data[<span>$name</span>]);
  }

  <span>/*
    类的对象的不存在的实例方法调用的时候,会自动调用本方法,该方法必须是public
   */</span><span>public</span><span><span>function</span><span>__call</span><span>(<span>$name</span> , <span>$arguments</span>)</span>{</span><span>if</span>(<span>$name</span>===<span>'f1'</span>){
        <span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"方法被调用,"</span>.<span>"参数:"</span>;
        var_dump(<span>$arguments</span>);
    }<span>else</span><span>if</span>(<span>$name</span>===<span>'f2'</span>){
        <span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"方法被调用,"</span>.<span>"参数:"</span>;
        var_dump(<span>$arguments</span>);
    }<span>else</span>{

        trigger_error(<span>"非法调用!"</span>,E_USER_WARNING);
    }

  }

  <span>/*
     类的对象的不存在的静态方法调用的时候,会自动调用本方法
     5.3.0 新增 __callStatic()魔术方法。可见性未设置为 public 或未声明为 static 的时候会产生一个警告 
   */</span><span>public</span><span>static</span><span><span>function</span><span>__callStatic</span><span>(<span>$name</span> , <span>$arguments</span>)</span>{</span><span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"静态方法被调用,"</span>.<span>"参数:"</span>;
        var_dump(<span>$arguments</span>);

  }

 }


<span>$s</span> = <span>new</span> Sample();

<span>echo</span><span>"<br>访问类中存在的实例属性:s->p1:: "</span>.<span>$s</span>->p1;


<span>//属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。从 PHP 5.3.0 起, 将这些魔术方法定义为 static 会产生一个警告。</span><span>//Fatal error: Access to undeclared static property: Sample::$p3</span><span>//echo "<br>访问类中不存在的静态属性不会调用魔术方法__get:".Sample::$p3;//错误</span><span>//访问类中不存在的属性,类中的魔术方法__get会被调用</span><span>echo</span><span>$s</span>->p2;

<span>//给类中不存在的属性赋值,类中的魔术方法__set会被调用</span><span>$s</span>->p2 = <span>88</span>;

<span>echo</span><span>'<br>'</span>.<span>$s</span>->p2; <span>//输出 88</span><span>//类中的魔术方法__isset会被调用</span>
var_dump(<span>isset</span>(<span>$s</span>->p2));
<span>//类中的魔术方法__isset会被调用</span>
var_dump(<span>empty</span>(<span>$s</span>->p2));

<span>//类中的魔术方法__unset会被调用</span><span>unset</span>(<span>$s</span>->p2);


<span>echo</span><span>'<br>'</span>.<span>$s</span>->p2; <span>//p2被销毁,报错</span><span>$s</span>->f1();
<span>$s</span>->f1(<span>1</span>,<span>2</span>);
<span>$s</span>->f1(<span>1</span>,<span>2</span>,<span>"hello"</span>);

<span>$s</span>->f2(<span>true</span>,<span>"111"</span>);

<span>$s</span>->f3();

<span>//调用对象不存在的静态方法</span>
Sample::say(<span>'hello'</span>);
Sample::say(<span>'hello'</span>,<span>'php'</span>);

<span>?></span></span></code>
Copy after login

static的作用,static electricity,static c语言,static 函数,java static,python static method,import static import区别,gcc static 链接静态库

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the overloading of php __get __set isset unset __call __callStatic, including static content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template