链方法能力无需强制使用php的新关键字



我正在开发一个必须像这样调用的脚本:

$father = Father::firstName('Esaaro')->lastName('Ozaaraa')->age(42);
Person::firstName("Soobaasaa")->lastName( "Ozaaraa")->age(17)
->setFather( $father )-> toArray();

所以我们有两个类PersonFather.

firstName两类的方法static方法,其他方法public

这是我的文件结构

<?php
class Person
{
protected static $name;
protected $lastName, $age, $father, $result;
public static function firstName($name = null)
{
self::$name = $name;
}
public function lastName($lastName = null)
{
$this->lastName = $lastName;
}
public function age($age = null)
{
$this->age = $age;
}
public function toArray()
{
}
public function setFather(Father $father)
{
}
}

/*
* Father Class
*/
class Father
{
protected static $name;
protected $family, $age, $result;
public static function firstName($name = null)
{
self::$name = $name;
}
public function lastName($lastName = null)
{
$this->family = $lastName;
}
public function age($age = null)
{
$this->age = $age;
}
public function toArray()
{
( (isset(static::$name) && static::$name !== null) ? $this->result['firsName'] = self::$name : '' );
( (isset($this->family) && $this->family !== null) ? $this->result['lastName'] = $this->family : '' );
return $this->result;
}
}

上面的代码只是结构,我刚刚开始研究脚本。文件结构无法更改,因为这是一个挑战。

我应该如何操作我的脚本,我可以像我之前提到的那样调用方法?

提前致谢

实际上,您所需要的只是静态firstName方法来创建类的新实例并返回它。

其他二传手只需要返回$this即可提供所谓的流畅接口

如果创建实例的唯一方法是通过静态firstName方法,则还需要添加私有/受保护的构造函数。

例如

class Person
{
private $firstName;
private $lastName;
private $age;
private $father;
private function __construct(string $firstName) {
$this->firstName = $firstName;
}
public static function firstName(string $name) {
return new Person($name);
}
public function lastName(string $lastName) {
$this->lastName = $lastName;
return $this;
}
public function age(int $age) {
$this->age = $age;
return $this;
}
public function setFather(Father $father) {
$this->father = $father;
return $this;
}
public function toArray() {
// just an example
return [
'firstName' => $this->firstName,
'lastName'  => $this->lastName,
'age'       => $this->age,
'father'    => $this->father->toArray(),
];
}
}

我强烈建议不要将$name属性保持为静态。您不想更改一个实例的$name并让它更改所有其他实例。这就是为什么我在上面的示例中将其更改为private $firstName的原因。

我知道这是一个挑战,所以你可能不得不使用特定的类结构。但是还有另一种方法可以做到这一点:

class Father
{
protected static $instance;
protected $firstName = '';
protected $lastName = '';
public function __call($name, $arguments)
{
if (property_exists($this, $name)) {
$this->$name = array_shift($arguments);
}
return $this;
}
public static function __callStatic($name, $arguments)
{
if (is_null(static::$instance)) {
static::$instance = new static;
}
return static::$instance->$name(...$arguments);
}
public function toArray()
{
return [
'firstName' => $this->firstName,
'lastName' => $this->lastName
];
}
}

在此版本中,您可以将以受保护变量命名的函数调用为静态或非静态。 因此,您可以Father::firstName('dad')->lastName('saray')做,反之亦然,因为Father::lastName('saray')->firstName('dad')。 为了简洁起见,我省略了年龄等其他方法。

根据评论中的要求,发生的情况如下:

当您将firstName()lastName()称为静态时,将运行魔术方法__callStatic()。如果它检测到没有实例,它将创建一个实例,调用该方法并返回实例。 然后,所有将来的调用都将由魔术方法__call()处理。 这将设置属性并返回相同的实例。 现在,这意味着您可以先将firstName()lastName()称为静态调用,然后所有后续调用都将是非静态的。

最新更新