Php 以逻辑顺序执行一致的功能

Php 以逻辑顺序执行一致的功能,php,Php,也许我在标题中表达的是错误的,但我只是不明白在课堂上怎么会这样 <?php class sample{ public $data = []; public function pushIndex($index){ array_push($this->data, $index); } public function pushValue($value){ array_

也许我在标题中表达的是错误的,但我只是不明白在课堂上怎么会这样

<?php
    class sample{
        public $data = [];

        public function pushIndex($index){
            array_push($this->data, $index);
        }

        public function pushValue($value){
            array_push($this->data["index"], $value);

            // Some magic
        }

        public function forIndex($index){
            return $this->data[$index];

            // Some magic
        }
    }

你所说的叫做。
使用$this返回当前对象

public function pushIndex($index){
    array_push($this->a,$index);
    return $this;
}
但你想做的是这样的事情:

class sample
{
    protected $a = [];
    protected $currentIndex = null;

    public function pushIndex($index)
    {
        $this->currentIndex = $index;
        return $this;
    }

    public function pushValue($value)
    {
        if ($this->currentIndex === null) {
            throw new LogicException('You need to call "pushIndex" or "forIndex" first.');
        }

        $this->a[$this->currentIndex] = $value;
        return $this;
    }

    public function forIndex($index)
    {
        if (!isset($this->a[$index])) {
            throw new RuntimeException(sprintf('Index "%s" doesn\'t exists', $index));
        }

        $this->currentIndex = $index;
        return $this;
    }

    public function getArray()
    {
        return $this->a;
    }
}
class sample{
    public $a = [];
    public $index = null;
    public function pushIndex($index){
        $this->index = $index;
        $this->a[$index] = null;
        return $this;
    }
    public function pushValue($value){
        $this->a[$this->index] = $value;
        return $this;
    }
    public function forIndex($index){
        $this->index = $index;
        return $this;
    }
}


$a = new sample;
$a->pushIndex("index")->pushValue("value");
$a->forIndex("index2")->pushValue("value2");

echo "<pre>";
var_dump($a);
echo "</pre>";

但是你想要什么还不清楚。

我认为你想要实现的是这样的:

class sample
{
    protected $a = [];
    protected $currentIndex = null;

    public function pushIndex($index)
    {
        $this->currentIndex = $index;
        return $this;
    }

    public function pushValue($value)
    {
        if ($this->currentIndex === null) {
            throw new LogicException('You need to call "pushIndex" or "forIndex" first.');
        }

        $this->a[$this->currentIndex] = $value;
        return $this;
    }

    public function forIndex($index)
    {
        if (!isset($this->a[$index])) {
            throw new RuntimeException(sprintf('Index "%s" doesn\'t exists', $index));
        }

        $this->currentIndex = $index;
        return $this;
    }

    public function getArray()
    {
        return $this->a;
    }
}
class sample{
    public $a = [];
    public $index = null;
    public function pushIndex($index){
        $this->index = $index;
        $this->a[$index] = null;
        return $this;
    }
    public function pushValue($value){
        $this->a[$this->index] = $value;
        return $this;
    }
    public function forIndex($index){
        $this->index = $index;
        return $this;
    }
}


$a = new sample;
$a->pushIndex("index")->pushValue("value");
$a->forIndex("index2")->pushValue("value2");

echo "<pre>";
var_dump($a);
echo "</pre>";
类示例{
公众$a=[];
public$index=null;
公共功能索引($index){
$this->index=$index;
$this->a[$index]=null;
退还$this;
}
公共功能价值($value){
$this->a[$this->index]=$value;
退还$this;
}
用于索引的公共函数($index){
$this->index=$index;
退还$this;
}
}
$a=新样本;
$a->pushIndex(“index”)->pushValue(“value”);
$a->forIndex(“index2”)->pushValue(“value2”);
回声“;
var_dump($a);
回声“;
这称为“方法链接”。通过返回对被调用对象的引用,您可以对该对象执行进一步的方法,本质上是“链接”这些方法


我已经对你的代码做了一些调整,让它按照你想要的方式工作。它应该提供一个工作示例来帮助您理解方法链接。

那么您尝试实现类似的功能了吗?如果是,为什么@Rizier123,我想以某种方式优化代码,以避免检查每个函数中的数据。该检查在一个位置完成,在第二个位置添加索引,在第三个位置添加其值!显然,这个用户更喜欢保持神秘的气氛,非常感谢。我现在至少明白了逻辑,没问题。按照现在编写的方式,您甚至可以将所有方法作为单个链调用。它应该适用于您链接到下一个的任何方法,因此它非常灵活。这就是方法链接的力量。非常感谢。我现在至少明白了逻辑。