在PHP中如何使用魔術(shù)方法-創(chuàng)新互聯(lián)

在PHP中如何使用魔術(shù)方法?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

馬尾網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,馬尾網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為馬尾上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的馬尾做網(wǎng)站的公司定做!

① __get/__set:將對(duì)象的屬性進(jìn)行接管

當(dāng)訪問(wèn)一個(gè)不存在的對(duì)象屬性時(shí):

index.php


復(fù)制代碼 代碼如下:


<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

$obj = new \Common\Object();

//在php中訪問(wèn)一個(gè)不存在的對(duì)象屬性時(shí)
echo $obj->title;


會(huì)拋出一個(gè)錯(cuò)誤:Notice: Undefined property: Common\Object::$title in D:\practise\php\design\psr0\index.php on line 9

當(dāng)在Common/Object.php 中添加 __set 和 __get 方法后

Object.php


復(fù)制代碼 代碼如下:


<?php
namespace Common;

class Object{
    function __set($key,$value){
    }
   
    function __get($key){
    }
}


再執(zhí)行 index.php,不會(huì)再報(bào)錯(cuò)。

再次修改 Common/Object.php


復(fù)制代碼 代碼如下:


<?php
namespace Common;

class Object{
    protected $array = array();
   
    function __set($key,$value){
        var_dump(__METHOD__);
        $this->array[$key] = $value;
    }
   
    function __get($key){
        var_dump(__METHOD__);
        return $this->array[$key];
    }
}

index.php


復(fù)制代碼 代碼如下:


<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

$obj = new \Common\Object();

$obj->title = 'hello';
echo $obj->title;


執(zhí)行 index.php,頁(yè)面輸出:


復(fù)制代碼 代碼如下:


string 'Common\Object::__set' (length=20)
string 'Common\Object::__get' (length=20)
hello


② __call/__callStatic:控制 PHP 對(duì)象方法的調(diào)用(__callStatic 用來(lái)控制類(lèi)的靜態(tài)方法)

當(dāng)執(zhí)行一個(gè)不存在的php方法時(shí)

index.php:


復(fù)制代碼 代碼如下:


<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

$obj = new \Common\Object();

//當(dāng)執(zhí)行一個(gè)不存在的php方法時(shí)
$obj->test('hello',123);


執(zhí)行 index.php 會(huì)報(bào)一個(gè)致命錯(cuò)誤:Fatal error: Call to undefined method Common\Object::test() in D:\practise\php\design\psr0\index.php on line 9

如果在 Common/Object 中定義一個(gè)__call 方法,則會(huì)在方法不存在時(shí)自動(dòng)回調(diào):


復(fù)制代碼 代碼如下:


<?php
namespace Common;

class Object{   
    function __call($func, $param){ //$func 方法名 $param 參數(shù)
        var_dump($func, $param);
        return "magic function\n"; //返回一個(gè)字符串作為返回值
    }
}


index.php

復(fù)制代碼 代碼如下:


<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

$obj = new \Common\Object();

//當(dāng)執(zhí)行一個(gè)不存在的php方法時(shí)
echo $obj->test('hello',123);


頁(yè)面輸出:


復(fù)制代碼 代碼如下:


string 'test' (length=4)
array
  0 => string 'hello' (length=5)
  1 => int 123
magic function


當(dāng)調(diào)用一個(gè)不存在的靜態(tài)方法時(shí)

Common/Object.php

復(fù)制代碼 代碼如下:


<?php
namespace Common;

class Object{
    static function __callStatic($name, $arguments) {
        var_dump($name, $arguments);
        return "magic function\n"; //返回一個(gè)字符串作為返回值       
    }
}


注意:__callStatic 方法也要聲明成靜態(tài)方法

index.php


復(fù)制代碼 代碼如下:


<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

//執(zhí)行一個(gè)不存在的靜態(tài)方法
echo Common\Object::test("hello",1234);


執(zhí)行 index.php ,頁(yè)面輸出:


復(fù)制代碼 代碼如下:


string 'test' (length=4)
array
  0 => string 'hello' (length=5)
  1 => int 1234
magic function


③ __toString:將一個(gè) PHP 對(duì)象轉(zhuǎn)換成一個(gè)字符串

index.php


復(fù)制代碼 代碼如下:


<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

$obj = new \Common\Object();

echo $obj;


此時(shí)會(huì)報(bào)錯(cuò): Catchable fatal error: Object of class Common\Object could not be converted to string in D:\practise\php\design\psr0\index.php on line 8

在 Object.php 中添加 __toString 方法


復(fù)制代碼 代碼如下:


<?php
namespace Common;

class Object{
    function __toString() {
        return __CLASS__;
    }
}


④ __invoke:將一個(gè) PHP 對(duì)象當(dāng)成一個(gè)函數(shù)來(lái)執(zhí)行時(shí),會(huì)回調(diào)此魔術(shù)方法

index.php


復(fù)制代碼 代碼如下:


<?php
define('BASEDIR',__DIR__); //定義根目錄常量
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');

$obj = new \Common\Object();

echo $obj("test");

Object.php


復(fù)制代碼 代碼如下:


<?php
namespace Common;

class Object{
    function __invoke($param) {
        var_dump($param);
        return 'invoke';
    }
}


頁(yè)面輸出:


復(fù)制代碼 代碼如下:


string 'test' (length=4)
invoke


關(guān)于在PHP中如何使用魔術(shù)方法問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前文章:在PHP中如何使用魔術(shù)方法-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://bm7419.com/article10/dsshdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、品牌網(wǎng)站制作、App設(shè)計(jì)、微信公眾號(hào)全網(wǎng)營(yíng)銷(xiāo)推廣外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作