中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php中魔術方法的使用示例

發布時間:2021-09-10 17:57:50 來源:億速云 閱讀:131 作者:chen 欄目:開發技術

本篇內容主要講解“php中魔術方法的使用示例”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“php中魔術方法的使用示例”吧!

復制代碼 代碼如下:


/** PHP把所有以__(兩個下劃線)開頭的類方法當成魔術方法。所以你定義自己的類方法時,不要以 __為前綴。 * */

// __toString、__set、__get__isset()、__unset()
/*
  The __toString method allows a class to decide how it will react when it is converted to a string.
  __set() is run when writing data to inaccessible members.
  __get() is utilized for reading data from inaccessible members.
  __isset() is triggered by calling isset() or empty() on inaccessible members.
  __unset() is invoked when unset() is used on inaccessible members.
 */
class TestClass {

    private $data = array();
    public $foo;

    public function __construct($foo) {
        $this->foo = $foo;
    }

    public function __toString() {
        return $this->foo;
    }

    public function __set($name, $value) {
        echo "__set, Setting '$name' to '$value'\n";
        $this->data[$name] = $value;
    }

    public function __get($name) {
        echo "__get, Getting '$name'\n";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
    }

    /** As of PHP 5.1.0 */
    public function __isset($name) {
        echo "__isset, Is '$name' set?\n";
        return isset($this->data[$name]);
    }

    /** As of PHP 5.1.0 */
    public function __unset($name) {
        echo "__unset, Unsetting '$name'\n";
        unset($this->data[$name]);
    }

}

$obj = new TestClass('Hello');
echo "__toString, $obj\n";
$obj->a = 1;
echo $obj->a . "\n\n";
var_dump(isset($obj->a));
unset($obj->a);
var_dump(isset($obj->a));
echo "\n\n";
/**
  輸出結果如下:
  __toString, Hello
  __set, Setting 'a' to '1'
  __get, Getting 'a'
  __isset, Is 'a' set?
  bool(true)
  __unset, Unsetting 'a'
  __isset, Is 'a' set?
  bool(false)
 **/

// __call  __callStatic
/*
  mixed __call ( string $name , array $arguments )
  mixed __callStatic ( string $name , array $arguments )
  __call() is triggered when invoking inaccessible methods in an object context.
  __callStatic() is triggered when invoking inaccessible methods in a static context.
  The $name argument is the name of the method being called.
  The $arguments argument is an enumerated array containing the parameters passed to the $name'ed method.
 */
class MethodTest {
    public function __call($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "__call, Calling object method '$name' " . implode(', ', $arguments) . "\n";
    }

    /** As of PHP 5.3.0 */
    public static function __callStatic($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "__callStatic, Calling static method '$name' " . implode(', ', $arguments) . "\n";
    }

}

$obj = new MethodTest;
$obj->runTest('in object context', 'param2', 'param3');
//MethodTest::runTest('in static context'); // As of PHP 5.3.0
echo "\n\n";
/**
 輸出結果如下:
 __call, Calling object method 'runTest' in object context, param2, param3
  string(10) "__invoke: "
 */

// __invoke
/*
  The __invoke method is called when a script tries to call an object as a function.
  Note: This feature is available since PHP 5.3.0.
*/
class CallableClass {
    function __invoke($x) {
        var_dump($x);
    }
}

$obj = new CallableClass;
//$obj(5);
var_dump('__invoke: ' . is_callable($obj));
echo "\n\n";

// __sleep  __wakeup
/*
  串行化serialize可以把變量包括對象,轉化成連續bytes數據. 你可以將串行化后的變量存在一個文件里或在網絡上傳輸.
  然后再反串行化還原為原來的數據. 你在反串行化類的對象之前定義的類,PHP可以成功地存儲其對象的屬性和方法.
  有時你可能需要一個對象在反串行化后立即執行. 為了這樣的目的,PHP會自動尋找__sleep和__wakeup方法.
  當一個對象被串行化,PHP會調用__sleep方法(如果存在的話). 在反串行化一個對象后,PHP 會調用__wakeup方法.
  這兩個方法都不接受參數. __sleep方法必須返回一個數組,包含需要串行化的屬性. PHP會拋棄其它屬性的值.
  如果沒有__sleep方法,PHP將保存所有屬性.下面的例子顯示了如何用__sleep和__wakeup方法來串行化一個對象.
  Id屬性是一個不打算保留在對象中的臨時屬性. __sleep方法保證在串行化的對象中不包含id屬性.
  當反串行化一個User對象,__wakeup方法建立id屬性的新值. 這個例子被設計成自我保持.
  在實際開發中,你可能發現包含資源(如圖像或數據流)的對象需要這些方法
 */

class User {

    public $name;
    public $id;

    function __construct() {
        //give user a unique ID 賦予一個差別 的ID
        $this->id = uniqid();
    }

    //__sleep返回值的類型是數組,數組中的值是不需要串型化的字段id

    function __sleep() {
        //do not serialize this->id 不串行化id
        return(array("name"));
    }

    function __wakeup() {
        //give user a unique ID
        $this->id = uniqid();
    }

}

//create object 成立一個器材
$u = new User;
$u->name = "Leon"; //serialize it 串行化 留意不串行化id屬性,id的值被遺棄
$s = serialize($u);
echo "__sleep, __wakeup, s: $s"; //unserialize it 反串行化 id被重新賦值
$u2 = unserialize($s); //$u and $u2 have different IDs $u和$u2有差別 的ID
print_r($u);
print_r($u2);
echo "\n\n";
/**
 輸出結果如下:
  __sleep, __wakeup, s: O:4:"User":1:{s:4:"name";s:4:"Leon";}
  User Object
  (
  [name] => Leon
  [id] => 4db1b17640da1
  )
  User Object
  (
  [name] => Leon
  [id] => 4db1b17640dbc
  )
 */


// __set_state
/*
  This static method is called for classes exported by var_export() since PHP 5.1.0.
  The only parameter of this method is an array containing exported properties in the form array('property' => value, ...).
 */

class A {

    public $var1;
    public $var2;

    public static function __set_state($an_array) { // As of PHP 5.1.0
        //$an_array打印出來是數組,而不是調用時傳遞的對象
        print_r($an_array);
        $obj = new A;
        $obj->var1 = $an_array['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }

}

$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';
echo "__set_state:\n";
eval('$b = ' . var_export($a, true) . ';');
// $b = A::__set_state(array(
// 'var1' => 5,
// 'var2' => 'foo',
// ));
var_dump($b);
echo "\n\n";
/**
  輸出結果如下:
  __set_state:
  Array
  (
  [var1] => 5
  [var2] => foo
  )
  object(A)#5 (2) {
  ["var1"]=>
  int(5)
  ["var2"]=>
  string(3) "foo"
  }
 */

// __clone
class SubObject {

    static $instances = 0;
    public $instance;

    public function __construct() {
        $this->instance = ++self::$instances;
    }

    public function __clone() {
        $this->instance = ++self::$instances;
    }

}

class MyCloneable {

    public $object1;
    public $object2;

    function __clone() {
        // Force a copy of this->object, otherwise
        // it will point to same object.
        $this->object1 = clone $this->object1;
    }

}

$obj = new MyCloneable();
$obj->object1 = new SubObject();
$obj->object2 = new SubObject();
$obj2 = clone $obj;
print("__clone, Original Object:\n");
print_r($obj);
print("__clone, Cloned Object:\n");
print_r($obj2);
echo "\n\n";
/**
 輸出結果如下:
 __clone, Original Object:
  MyCloneable Object
  (
  [object1] => SubObject Object
  (
  [instance] => 1
  ) [object2] => SubObject Object
  (
  [instance] => 2
  ))
  __clone, Cloned Object:
  MyCloneable Object
  (
  [object1] => SubObject Object
  (
  [instance] => 3
  ) [object2] => SubObject Object
  (
  [instance] => 2
  ))
 */

到此,相信大家對“php中魔術方法的使用示例”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

php
AI

东阳市| 牙克石市| 壶关县| 博罗县| 南木林县| 美姑县| 康平县| 浦北县| 桦川县| 宁南县| 克拉玛依市| 邓州市| 若尔盖县| 会宁县| 胶州市| 皋兰县| 中阳县| 桐柏县| 云和县| 阿巴嘎旗| 周宁县| 建宁县| 沛县| 巴林右旗| 肃北| 紫金县| 遂宁市| 焉耆| 芜湖市| 会同县| 延川县| 聂拉木县| 玉林市| 左云县| 冕宁县| 英德市| 博罗县| 迁西县| 松滋市| SHOW| 盐城市|