php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44930 protected __set()|__get() not working
Submitted: 2008-05-07 00:02 UTC Modified: 2008-05-07 15:08 UTC
From: daniel at txtconnect dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.6 OS: Linux (Ubuntu 7.10)
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: daniel at txtconnect dot com
New email:
PHP Version: OS:

 

 [2008-05-07 00:02 UTC] daniel at txtconnect dot com
Description:
------------
When using protected __set() it still gets called from outside the class (parent or children) 

Reproduce code:
---------------
abstract class Example {
    private $data = array();
    protected function __set($key, $value) {
        $this->data[$key] = $value;
    }
    protected function __get($key) {
        return $this->data[$key];
    }
}

class MyExample extends Example {
    public function __construct($name, $value) {
        $this->$name = $value; // sets parent::$data[$name] = $key
    }
}
$c = new MyExample('name', 'foo');
echo $c->name; // echoes foo (Should not do so because Example::__get() is protected)
$c->name = 'bar'; // Should not be able to assign 'bar' Example::__set() protected, but does!
echo $c->name; // echoes bar (Should not do so because Example::__get() are protected)
var_dump($c);

Expected result:
----------------
I expected an error to be thrown saying that i am trying to access protected data and am not allowed to do so.

When setting a variable, from outside the class, i should be unable to do so error thrown! 

Actual result:
--------------
foo
bar
object(MyExample)#1 (1) 
{ 
    ["data:private"]=> array(1) 
    { 
        ["name"]=> string(3) "bar" 
    }
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-07 03:40 UTC] crrodriguez at suse dot de
http://php.net/manual/en/language.oop5.overloading.php

"All overloading methods must be defined as public. "


but anyway.. your code is behaving as expected.
 [2008-05-07 15:08 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2011-01-19 17:52 UTC] theodor at tonum dot no
I'd give kudos to daniel for this one.

Yes, __get() and __set() is supposed to be set to public - but it would be a good 
thing, in terms of data-encapsulation, that we could have protected __get() and 
__set().

Dunno if this should be brought to feature request - but as the documentation 
states:
"All overloading methods must be defined as public. "

One should expect another result than the above by defining __get/__set as protected 
(e.g. a php error).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 03:01:33 2025 UTC