SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast duff_beer's Avatar
    Join Date
    Jul 2003
    Location
    up in the clouds
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    object delegate (was [$this = $that; and PHP4+5])

    Hi,

    I learn from #php.internals that $this = $that; will raise a fatal error with PHP5.

    PHP Code:
    class MyClass
    {
        function 
    MyClass($type$params)
        {
            if (
    $type='A') {
                
    $this = new A($params);
            } else {
                
    $this = new B($params);
            }
        }
    }

    class 
    A
    {
        function 
    A($params) {}
        function 
    doThis() {}
        function 
    doThat() {}
    }

    class 
    B
    {
        function 
    B($params) {}
        function 
    doThis() {}
        function 
    doThat() {}

    In this case, MyClass is a sort of a delegate for A and B, i.e. its only reason to exist is to instanciate the right class.

    How can the MyClass preserve its behaviour without changing the API *and* making it work under both PHP4 and PHP5?
    Basically, MyClass could act as a proxy, but I don't like the idea of redirecting every single method to the proxied class:

    PHP Code:
    class MyClass
    {
        var 
    $class null;
     
        function 
    MyClass($type$params)
        {
            if (
    $type='A') {
                
    $this->class = new A($params);
            } else {
                
    $this->class = new B($params);
            }
        }
        function 
    doThis() {
            return 
    $this->class->doThis();
        }    
        function 
    doThat() {
            return 
    $this->class->doThat();
        }

    Any other idea?
    Last edited by duff_beer; Feb 6, 2004 at 03:36.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a factory method return the right object instead?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Enthusiast duff_beer's Avatar
    Join Date
    Jul 2003
    Location
    up in the clouds
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Jason, thanks for you reply.

    Quote Originally Posted by sweatje
    Have a factory method return the right object instead?
    that would require an API change, wouldn't it?

    PHP Code:
    $myClass = new MyClass('A'$params);
    echo 
    $myClass->property
    that's how users call MyClass now, and that's how they need to call it tomorrow.
    They don't need to know there are two subclasses (A and B), and they should be able to access A or B properties like they were properties of MyClass.

    Could you write a small code snippet to illustrate your point?

    Thanks a lot

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are correct. If you are not allowed to touch $this, then using a factory method will require an API change.

  5. #5
    SitePoint Enthusiast duff_beer's Avatar
    Join Date
    Jul 2003
    Location
    up in the clouds
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Object Delegates

    Thanks Jason.

    Slightly changing the post topic, I'd like to collect ideas on how to implement an object delegate in PHP4, without using extensions not in the default package.

    Returning to the example in the first post, "MyClass" is a clear example of a delegate object. It just returns an object of the desired class.

    To make it clearer, let's change the above example to this one:

    PHP Code:
    class MyClass 

        function 
    MyClass($type$params
        { 
            if (!@include_once(
    $type.'.php')) {
                
    $type 'A';
                include_once(
    'A.php');
            }
            
    $this = new $type($params); 
        }     

    This class behaviour can be emulated with proxy calls to each method of the A or B classes, but it has two drawbacks:

    1) a lot of code is needed just to proxy method calls:
    PHP Code:
        function doThis() { 
            return 
    $this->class->doThis(); 
        } 
    2) it's not extensible, i.e. if class "C" has a new method "doThisOtherThing()", and we don't update class "MyClass" accordingly, the last method will not be accessible.

    So, the final question is:
    How can we define a delegate class in PHP4?
    or, if you prefer:
    How can we define a "lazy proxy" without using the "__call()" function?

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might want to take a look at Marcus's post (#5) in this thread.

  7. #7
    SitePoint Enthusiast duff_beer's Avatar
    Join Date
    Jul 2003
    Location
    up in the clouds
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    had seen that, but it relies on "__call()" and "overload()"...


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
  •