Is this a reasonable way to handle getters/setters in a PHP class?

前端 未结 8 1270
無奈伤痛
無奈伤痛 2020-12-16 03:50

I\'m going to try something with the format of this question and I\'m very open to suggestions about a better way to handle it.

I didn\'t want to just dump a bunch o

相关标签:
8条回答
  • 2020-12-16 04:06

    Just recently, I also thought about handling getters and setters the way you suggested (the second approach was my favorite, i.e. the private $props array), but I discarded it for it wouldn't have worked out in my app.

    I am working on a rather large SoapServer-based application and the soap interface of PHP 5 injects the values that are transmitted via soap directly into the associated class, without bothering about existing or non-existing properties in the class.

    0 讨论(0)
  • 2020-12-16 04:09

    @Brian

    My problem with this is that adding "more logic later" requires that you add blanket logic that applies to all properties accessed with the getter/setter or that you use if or switch statements to evaluate which property you're accessing so that you can apply specific logic.

    That's not quite true. Take my first example:

    class PropTest extends PropertyHandler
    {
        public function __construct()
        {
            parent::__construct();
        }
    }
    
    $props = new PropTest();
    
    $props->setFirstName("Mark");
    echo $props->getFirstName();
    

    Let's say that I need to add some logic for validating FirstNames. All I have to do is add a setFirstName method to my subclass and that method is automatically used instead.

    class PropTest extends PropertyHandler
    {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function setFirstName($name)
        {
            if($name == 'Mark')
            {
                echo "I love you, Mark!";
            }
        }
    }
    

    I'm just not satisfied with the limitations that PHP has when it comes to implicit accessor methods.

    I agree completely. I like the Python way of handling this (my implementation is just a clumsy rip-off of it).

    0 讨论(0)
  • 2020-12-16 04:14

    @Mark

    But even your method requires a fresh declaration of the method, and it somewhat takes away the advantage of putting it in a method so that you can add more logic, because to add more logic requires the old-fashioned declaration of the method, anyway. In its default state (which is where it is impressive in what it detects/does), your technique is offering no advantage (in PHP) over public fields. You're restricting access to the field but giving carte blanche through accessor methods that don't have any restrictions of their own. I'm not aware that unchecked explicit accessors offer any advantage over public fields in any language, but people can and should feel free to correct me if I'm wrong.

    0 讨论(0)
  • 2020-12-16 04:14

    I can't help putting in my 2 cents...

    I have taken to using __get and __set in this manor http://gist.github.com/351387 (similar to the way that doctrine does it), then only ever accessing the properties via the $obj->var in an outside of the class. That way you can override functionality as needed instead of making a huge __get or __set function, or overriding __get and __set in the child classes.

    0 讨论(0)
  • 2020-12-16 04:17

    The way I do it is the following:

    class test {
        protected $x='';
        protected $y='';
    
        function set_y ($y) {
            print "specific function set_y\n";
            $this->y = $y;
        }
    
        function __call($function , $args) {
            print "generic function $function\n";
            list ($name , $var ) = split ('_' , $function );
            if ($name == 'get' && isset($this->$var)) {
                return $this->$var;
            }
            if ($name == 'set' && isset($this->$var)) {
                $this->$var= $args[0];
                return;
            }
            trigger_error ("Fatal error: Call to undefined method test::$function()");
        }
    }
    
    $p = new test();
    $p->set_x(20);
    $p->set_y(30);
    print $p->get_x();
    print $p->get_y();
    
    $p->set_z(40);
    

    Which will output (line breaks added for clarity)

    generic function set_x
    specific function set_y
    
    generic function get_x
    20
    generic function get_y
    30
    
    generic function set_z
    Notice: Fatal error: Call to undefined method set_z() in [...] on line 16
    
    0 讨论(0)
  • 2020-12-16 04:18

    Yes that's right the variables have to be manually declared but i find that better since I fear a typo in the setter

    $props2->setFristName('Mark');
    

    will auto-generate a new property (FristName instead of FirstName) which will make debugging harder.

    0 讨论(0)
提交回复
热议问题