PHP魔術方法的使用樣本,php魔術樣本_PHP教程

來源:互聯網
上載者:User

PHP魔術方法的使用樣本,php魔術樣本


① __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

http://www.bkjia.com/PHPjc/1021094.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1021094.htmlTechArticlePHP魔術方法的使用樣本,php魔術樣本 ① __get/__set:將對象的屬性進行接管 當訪問一個不存在的對象屬性時: index.php 複製代碼 代碼如下:...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.