国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP 接口實際上如何改變類的行為
P粉289775043
P粉289775043 2023-09-02 23:49:28
0
1
714
<p>根據(jù) PHP 文檔,</p> <blockquote> <p>對象接口允許您創(chuàng)建指定類必須實現(xiàn)哪些方法的代碼,而無需定義這些方法的實現(xiàn)方式。</p> </blockquote> <p>因此,接口就像一個具有預定義方法的類,仍然需要使用 <code>-></code> 符號來訪問</p> <p>但是,ArrayAccess 接口提供對對象作為數(shù)組的訪問。可以使用 <code>$object->property</code> 和 <code>$object["property"]</code></p> 訪問對象 <p>我無法理解 ArrayAccess 如何使更改對象語法成為可能。我編寫了一段代碼來嘗試復制 <code>ArrayAccess</code> 方法<em>僅一個</em>的效果,但它拋出錯誤</p> <pre class="brush:php;toolbar:false;">// Using the PHP ArrayAccess Interface namespace A { class myclass implements \ArrayAccess { public function offsetExists($offset) { return true; } public function offsetGet($offset) { // changed behaviour return $this->{$offset} ?? null; } public function offsetSet($offset, $value) {} public function offsetUnset($offset) {} } $myclass = new myclass(); $myclass->access = 'Interface'; echo $myclass['access']; // "Interface" }; //Trying to implement my own ArrayAccess Interface namespace B { interface MyArrayAccess { public function offsetGet($offset); } class myclass implements MyArrayAccess { public function offsetGet($offset) { // change behaviour return $this->{$offset} ?? null; } } $myclass = new myclass(); $myclass->access = 'Interface'; echo $myclass['access']; // Fatal error: Uncaught Error: Cannot use object of type B\myclass as array } </pre> <p>請幫我正確解釋一下。謝謝</p>
P粉289775043
P粉289775043

全部回復(1)
P粉702946921

我并不是說接口“改變了類的行為”,而是說接口使擴展類功能變得容易。

要理解接口,作為一個面向?qū)ο蟮木幊谈拍?,我們首先應該理解它要解決什么問題。

“Interface”旨在解決什么問題?

接口是一種契約。這是在 PHP 中實現(xiàn) duck-typing 的方法。您需要從庫編寫者的角度思考,他希望向其他人公開功能。例如,

class Greeter
{
    public function greet($person)
    {
        echo "Hello, {$person->getName()}!\n";
    }
}

為了確保庫用戶知道 $person 需要有 getName() 方法,您可以創(chuàng)建一個類 Person > 有一個 getName() 方法。然后使用 類型聲明 來檢測代碼執(zhí)行時的潛在錯誤已解析。

class Greeter
{
    public function greet(Person $person)
    {
        echo "Hello, {$person->getName()}!\n";
    }
}

class Person
{
    public string $name;
    public function getName(): string
    {
        return $this->name;
    }
}

假設有另一個庫可以用食物來喂養(yǎng)東西:

class Feeder {
    public function feed(Eater $eater, string $food) {
        $eater->eat($food);
    }
}

class Animal {
    private $stomach = [];
    public function eat(string $food) {
        $stomach = $food;
    }
}

考慮這個...

現(xiàn)在,假設用戶想要編寫一個既可以吃飯又可以打招呼的 Pet 類。用戶不想僅僅為了 Pet 再次編寫這些功能。

如何編寫 Pet 以便同時使用 GreeterFeeder 庫?

也許是這樣的?

class Pet extends Person, Animal {
}

不幸的是,PHP 不支持多重繼承。一個類只能擴展一個類。上面的代碼無效。所以在目前的情況下,用戶只能使用其中一個庫。

此外,對于不同的事物,“名稱”可能是一個非常不同的概念(例如,一個人可能會使用 getName() 返回 $first_name$last_name 代碼>)。您的庫類中可能沒有合理的默認實現(xiàn) getName() 方法。

所以,作為一名庫編寫者,您希望他/她的庫對用戶盡可能靈活。你能做什么?

如何用 PHP 中的“接口”解決這個問題?

接口是方法簽名的聲明。這是在沒有具體類/繼承要求的情況下聲明庫要求的快捷方式。

使用接口,您可以像這樣重寫兩個庫:

Greeter

class Greeter {
    public function greet(Namer $namer) {
        echo "Hello, {$namer->getName()}!\n";
    }
}

interface Namer {
    public function getName(): string;
}

Feeder

class Feeder {
    public function feed(Eater $eater, string $food) {
        $eater->eat($food);
    }
}

interface Eater {
    public function eat(string $food);
}

不需要一個具體的類(或父類繼承),一個類可以實現(xiàn)多個接口。所以下面的 Pet 類在 PHP 中是完全有效的:

class Pet implements Namer, Eater {
    private array $stomach = [];
    private string $name = '';

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

    /**
     * Implements Namer.
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * Implements Eater.
     */
    public function eat(string $food)
    {
        $this->stomach[] = $food;
    }
}

$greeter = new Greeter();
$feeder = new Feeder();
$pet = new Pet('Paul');

$greeter->greet($pet);
$feeder->feed($pet, 'a biscuit');

現(xiàn)在,此 Pet 類的對象可以與 Greeter 庫和 Feeder 庫一起使用。

ArrayAccess 接口怎么樣?

ArrayAccess 接口不是由第三方聲明的接口庫編寫者,但由核心 PHP 編寫者編寫。核心PHP編寫器對此提供了更深刻的支持。

有點像我們之前提到的接口,PHP 為實現(xiàn)它的類提供功能。但核心 PHP 不是提供上面的 GreeterFeeder 示例,而是提供 實現(xiàn) ArrayAccess 的類的語法糖。這意味著您可以使用更簡潔的代碼來處理實現(xiàn) AccessAccess 接口的類。

在官方示例中,

<?php
class Obj implements ArrayAccess {
    private $container = array();

    public function __construct() {
        $this->container = array(
            "one"   => 1,
            "two"   => 2,
            "three" => 3,
        );
    }

    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }

    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }

    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }

    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
}

如果您實現(xiàn)了它們,則代替這些:

$obj = new Obj;
$obj->offsetSet(10, "hello");
$obj->offsetSet(11, "world");
if ($obj->offsetUnset(12)) {
    $obj->offsetUnset(12);
}
echo $obj->offsetGet(11);

您可以將 $obj 與類似數(shù)組的語法一起使用,以使代碼更短:

$obj = new Obj;

$obj[10] = "hello";
$obj[11] = "world";
if (isset($obj[12])) {
    unset($obj[12]);
}
echo $obj[11];
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板