Maison >développement back-end >tutoriel php >php之重载__get __set isset unset __call __callStatic python static method import static import区别 gcc static 链接静态库
php中的overloading跟传统面向对象的重写不同,例如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>
php中重载是对类或者对象调用不存在的属性或方法时一种“优雅”的错误处理机制。想不明白为什么PHP这也叫重载,跟传统面向对象编程中的重载可以说是风马牛不相及。
php的重载依靠魔术方法__get() __set() isset() unset() __call() __callStatic()来实现,在我们访问不存在的属性或者方法时候,系统会自动调用这些魔术方法。
代码:
<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></span></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了php之重载__get __set isset unset __call __callStatic,包括了static方面的内容,希望对PHP教程有兴趣的朋友有所帮助。