php - 如何在方法之间共享行为

标签 php oop

我有一组 getter/setter 方法来与我的类属性交互,这里有两个类似的方法:

public function Criteres ($name = '',$value = false)
 {if ($name == '') return $this->Criteres;
  else if ($value !== false && strlen($name)) $this->Criteres[$name] = $value;
  else if (strlen($name)) return (isset($this->Criteres[$name]) ? $this->Criteres[$name] : '');}

public function Navigation ($name = '',$value = false)
 {if ($name == '') return $this->Navigation;
  else if ($value !== false && strlen($name)) $this->Navigation[$name] = $value;
  else if (strlen($name)) return (isset($this->Navigation[$name]) ? $this->Navigation[$name] : '');}

它们具有完全相同的行为,唯一的变化是它们所作用的属性。我应该提到的是,正在访问的属性实际上是包含多个属性的数组

有没有一种方法可以让它们扩展为一种抽象方法,使它们的行为相同,而无需每次都定义行?

或者任何其他扩展方式,我并不关心。

例如,我想要的结果是这样的:

ps。我知道这不是我们使用抽象类的方式,abstract将是某种关键字来帮助php理解定义的类是一个模板

abstract function setterGetter ($name = '',$value = false)
 {if ($name == '') return $this->{PROP};
  else if ($value !== false && strlen($name)) $this->{PROP}[$name] = $value;
  else if (strlen($name)) return (isset($this->{PROP}[$name]) ? $this->{PROP}[$name] : '');}

然后,为属性定义自定义 setter/getter:

public function Criteres extends setterGetter ({Criteres})
 {}
public function Navigation extends setterGetter ({Navigation})
 {} // without the need to define the code, they all act the same

最佳答案

您不能扩展方法,也不能拥有由具有不同名称的两个不同方法实现的抽象方法。毕竟抽象类和接口(interface)应该提供一种独特的方法来寻址函数。

但是,如果 CriteresNavigation 都是同一类的方法/成员,则可以为这些引用的对象编写通用的 setter/getter。例如:

class MyData {

    protected $criteres;
    protected $navigation;

    public function objectHelper( $type, $name = null, $value = null )
    {
        $possibleTypes = array( 'Criteres', 'Navigation' );

        if( !in_array( $type, $possibleTypes ) )
        {
            throw new InvalidArgumentException( 'There is no such child object as ' . $type );
        }

        $type = strtolower( $type );

        if( empty( $name ) )
        {
            // getter
            return $this->$type;
        }
        elseif( !empty( $value ) )
        {
            /* setter
             * Attention: there no check at this point whether the key $name exists. */
            $this->$type[$name] = $value;
        }
        elseif( isset( $this->$type[$name] ) )
        {
            // specific getter
            return $this->$type[$name];
        }
        else
        {
            return null;
        }
    }

    /**
     * PHP magic function to use direct name notation
     *
     * Example:
     * 
     *   $myData->Navigation( 'name', 'value' );
     */
    public function __call( $prop, $args )
    {
        return $this->objectHelper(
            $prop,
            isset( $args[0] ) ? $args[0] : null,
            isset( $args[1] ) ? $args[1] : null
        );
    }

}

提示:在 PHP 中,习惯上对不需要的函数参数使用 null

我会尽量避免混合使用 setter 和 getter。您的代码将更具可读性、可测试性,并且可能具有更高的性能。

关于php - 如何在方法之间共享行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24435886/

相关文章:

java - 抽象类中的私有(private)实例变量

php - 如何在我的文本中间获取某个字符串

php - 循环直到变量达到 0

php - 使用 PHP 针对 Schematron 验证 XML

javascript - 类似 Java/C# 的类定义在 Javascript 中不起作用吗?

java - 面向对象编程和Java : how to generalize from an abstract class?

c++ - 模板类与抽象类

php - 如何在除一个子页面之外的每个子页面上显示一些 html?

php - AJAX + PHP : Session timeout

C++ 使用 scoped_ptr 作为成员变量