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></code>
以上就介紹了php之重載__get __set isset unset __call __callStatic,包括了static方面的內容,希望對PHP教程有興趣的朋友有所幫助。