简体   繁体   English

如何在PHP的类属性中指定get和set方法/函数?

[英]How do I specify get and set methods/functions as part of a class' property in PHP?

Using PHP, how do I define/declare getter and setter methods/functions as part of the declaration of a property in a class? 使用PHP,如何在类的属性声明中定义/声明getter和setter方法/函数?

What I'm trying to do is specify the getter and setter methods as part of a property, instead of declaring separate set_propertyName($value) and get_propertyName() functions/methods. 我想做的是将getter和setter方法指定为属性的一部分,而不是声明单独的set_propertyName($value)get_propertyName()函数/方法。

What I've got: 我所拥有的:

class my_entity {
    protected $is_new;
    protected $eid; // entity ID for an existing entity
    public function __construct($is_new = FALSE, $eid = 0) {
        $this->is_new = $is_new;
        if ($eid > 0) {
            $this->set_eid($eid);
        }
    }

    // setter method
    public function set_eid($eid) {
        $is_set = FALSE;
        if (is_numeric($eid)) {
            $this->eid = intval($eid);
            $is_set = TRUE;
        }
        return $is_set;
    }
}

What I want (without making $this->eid an object): 我想要什么(不使$ this-> eid成为对象):

class my_entity {
    protected $is_new;
    // entity ID for an existing entity
    protected $eid {
      set: function($value) {
        $is_set = FALSE;
        if (is_numeric($value)) {
            $this->eid = intval($value);
            $is_set = TRUE;
        }
        return $is_set;

      }, // end setter

    }; 
    public function __construct($is_new = FALSE, $eid = 0) {
        $this->is_new = $is_new;
        if ($eid > 0) {
            $this->set_eid($eid);
        }
    }

    // setter method/function removed
}

PHP only allows one getter and one setter function per class, they are the __get & __set magic methods. PHP每个类仅允许一个getter和一个setter函数,它们是__get__set魔术方法。 These two magic methods must handle get and set requests for all private/inaccessible properties. 这两个魔术方法必须处理所有私有/不可访问属性的获取和设置请求。 http://www.php.net/manual/en/language.oop5.magic.php http://www.php.net/manual/en/language.oop5.magic.php

private function set_eid($id)
{
    //set it...
    $this->eid = $id;
}

private function get_eid($id)
{
    //return it...
    return $this->eid;
}

public function __set($name, $value)
{
    switch($name)
    {
        case 'eid':
            $this->set_eid($value);
        break;
    }
}

public function __get($name)
{
    switch($name)
    {
        case 'eid':
            return $this->get_eid();
        break;
    }
}

In the 2 switch statements you could also add the names of other properties. 在2条switch语句中,您还可以添加其他属性的名称。

It's important to remember that __get and __set are only called if the variable is inaccessible, this means that when getting or setting from inside the class you will still have to manually call set__eid . 重要的是要记住,只有在变量不可访问时才调用__get__set ,这意味着从类内部进行获取或设置时,您仍将必须手动调用set__eid

This was proposed for PHP 5.5, but the vote failed to get the necessary 2/3 majority required to accept it into core, so it won't be implemented (despite the code to implement that change having been submitted). 这是为PHP 5.5 提出的,但是投票未能获得接受它的2/3多数票,因此无法实现(尽管已提交实现该更改的代码)。

It's entirely possible (with the plethora of new PHP engines and Hacklang currently appearing) that it will be resubmitted at a future time, especially if Hacklang decides to implement it; 完全有可能(随着大量新PHP引擎和Hacklang的出现)将来会重新提交它,特别是如果Hacklang决定实现它。 but for the moment the option of using C# getters/setters isn't available in PHP 但是目前在PHP中没有使用C#getters / setters的选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM