SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP5 overloading, cannot access protected property

    hi everyone,

    im using overloading, especially __get(), to simplify chaining of objects like this
    PHP Code:
    $this->context->user->attributes->userID 1
    now i get this error-message:
    Code:
    "Fatal error: Cannot access protected property Radix_Context::$user in <file> on line <line>"
    context has an protected attribute $user, so php should trigger __get() to dispatch this call but it doesn't! it tries to access the property directly which does not work.

    on the other hand, this testcase works like a charm:
    PHP Code:
    class OverloadingDispatcher
    {
    function 
    __get($name)
        {
        return 
    call_user_func(array($this "get{$name}"));
        }
    }


    class 
    Root extends OverloadingDispatcher
    {
    protected 
    $a;

    function 
    __construct()
        {
        
    $this->= new A();
        }

    function 
    getA()
        {
        return 
    $this->a;
        }
    }

    class 
    extends OverloadingDispatcher
    {
    protected 
    $b;

    function 
    __construct()
        {
        
    $this->= new B();
        }

    function 
    getB()
        {
        return 
    $this->b;
        }
    }

    class 
    extends OverloadingDispatcher
    {
    protected 
    $c;

    function 
    __construct()
        {
        
    $this->= new C();
        }

    function 
    getC()
        {
        return 
    $this->c;
        }
    }

    class 
    C
    {
    function 
    sayHello()
        {
        echo 
    'hello world!';
        }
    }

    $r = new Root();
    $r->a->b->c->sayHello(); 
    i already sent this problem to the german mailinglist located at phpbar.de and johannes schlueter answered, stating that this procedure is absolutely normal.
    php checks the properties visibility and than refuses to call the magic-overloading-functions.

    anyone any hint?
    thanks!
    kai

  2. #2
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is true, php checks for property visibility BEFORE it checks for overloading. Nothing u can do about it except use __call() instead and use them as method, ie:

    $class1->class2()->class3()->class4()->class5()

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k, i could understand this BUT why does the testcase work like a charm ???

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump*

    im using php5.1 RC1.
    anyone any hint?

    thanks,
    kai

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kaimeder
    *bump*
    You'll probably get your question answered quicker if you post a minimal code that doesn't work.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried to build a testcase with pretty much the same class-hierachy as in my non-working code but the bug does not appear, so i can't reproduce it....

    i may post the complete code of my application but this is far too much ...

    so ... rephrasing my question: is my problem at least _known_ to others? (using php5.1RC1)

    yours,
    kai

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k, now i know the error.

    you can't use __set() whiling chaining, only __get()-calls work ...

    does NOT work:
    PHP Code:
    $class1->class2->class3->foobar 1
    does work:
    PHP Code:
    echo $class1->class2->class3->foobar
    ....
    kai

  8. #8
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kaimeder
    you can't use __set() whiling chaining, only __get()-calls work ...
    I assume you mean if you have an actual variable that's named the same as the virtual one you're trying to access. It seems to me it would be simpler to rename the real one to, say, $_var instead of $var;
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it's a bug related to bug 28444.

    You should submit a bugreport (seems it's not yet reported)

    Code:
    --reproduce code
    
    class A {
    	private $p;
    	function __get($name){
    		return $this->$name;
    	}
    	function __set($name, $value) {
    		$this->$name = $value;
    	}
    }
    class B {
    	private $t;
    	function __get($name){
    		return $this->$name;
    	}
    	function __set($name, $value) {
    		$this->$name = $value;
    	}
    }
    $a = new A;
    $b = new B;
    $a->p = $b;
    $b->t = "foo";
    
    echo $a->p->t;
    $a->p->t = "bar";
    echo $a->p->t;
    
    --expected result:
    
    foobar
    
    --actual result
    
    foo
    Fatal error: Cannot access private property A::$p in...

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    hamburg, germany
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thats *exactly* what im meaning, thanks.
    i've posted a bug-report using your code.

    thanks!
    kai

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The testcase above works with last cvs.
    kaimeder: thanks for help improving php

  12. #12
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a problem that's been affecting prado for quite a while.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •