国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP魔術方法的使用示例

2024-05-04 23:36:46
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了PHP魔術方法的使用示例,本文分別講解了__get、__set、__call、__callStatic、__toString、 __invoke等魔術方法的使用,需要的朋友可以參考下
 

① __get/__set:將對象的屬性進行接管

當訪問一個不存在的對象屬性時:

index.php

復制代碼代碼如下:

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

 

$obj = new /Common/Object(); 

//在php中訪問一個不存在的對象屬性時
echo $obj->title;

 

會拋出一個錯誤:Notice: Undefined property: Common/Object::$title in D:/practise/php/design/psr0/index.php on line 9

當在Common/Object.php 中添加 __set 和 __get 方法后

Object.php

復制代碼代碼如下:

<?php
namespace Common;

 

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

 

再執行 index.php,不會再報錯。

再次修改 Common/Object.php

復制代碼代碼如下:

<?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
復制代碼代碼如下:

<?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;

 

執行 index.php,頁面輸出:

復制代碼代碼如下:

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

 

② __call/__callStatic:控制 PHP 對象方法的調用(__callStatic 用來控制類的靜態方法)

當執行一個不存在的php方法時

index.php:

復制代碼代碼如下:

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

 

$obj = new /Common/Object(); 

//當執行一個不存在的php方法時
$obj->test('hello',123);

 

執行 index.php 會報一個致命錯誤:Fatal error: Call to undefined method Common/Object::test() in D:/practise/php/design/psr0/index.php on line 9

如果在 Common/Object 中定義一個__call 方法,則會在方法不存在時自動回調:

復制代碼代碼如下:

<?php
namespace Common;

 

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

 

index.php

 

復制代碼代碼如下:

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

 

$obj = new /Common/Object(); 

//當執行一個不存在的php方法時
echo $obj->test('hello',123);

 

頁面輸出:

復制代碼代碼如下:

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

 

當調用一個不存在的靜態方法時

Common/Object.php

 

復制代碼代碼如下:

<?php
namespace Common;

 

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

 

注意:__callStatic 方法也要聲明成靜態方法

index.php

復制代碼代碼如下:

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

 

//執行一個不存在的靜態方法
echo Common/Object::test("hello",1234);

 

執行 index.php ,頁面輸出:

復制代碼代碼如下:

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

 

③ __toString:將一個 PHP 對象轉換成一個字符串

index.php

復制代碼代碼如下:

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

 

$obj = new /Common/Object();

echo $obj;

 

此時會報錯: 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 方法

復制代碼代碼如下:

<?php
namespace Common;

 

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

 

④ __invoke:將一個 PHP 對象當成一個函數來執行時,會回調此魔術方法

index.php

復制代碼代碼如下:

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

 

$obj = new /Common/Object();

echo $obj("test");


Object.php
復制代碼代碼如下:

<?php
namespace Common;

 

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

 

頁面輸出:

復制代碼代碼如下:

string 'test' (length=4)
invoke

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉林省| 黔西| 沙坪坝区| 霍邱县| 福州市| 北票市| 武平县| 九江县| 裕民县| 民权县| 吴桥县| 仪陇县| 乌拉特后旗| 石狮市| 凉山| 莎车县| 建始县| 定南县| 卓资县| 福建省| 微山县| 涟水县| 杨浦区| 新津县| 肇庆市| 多伦县| 汝阳县| 赤水市| 武安市| 衡阳县| 宝兴县| 西乌| 揭东县| 哈巴河县| 元朗区| 新巴尔虎左旗| 方山县| 屏东市| 祁东县| 凤庆县| 莱西市|