SitePoint Sponsor

User Tag List

Page 4 of 4 FirstFirst 1234
Results 76 to 90 of 90

Thread: inheritance

  1. #76
    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 Etnu
    This should be resolved soon. Check PHP internals mailing list.
    You're probably refering to this thread
    http://groups.google.com/group/maili...f2b34cd7c85d02

    Quote Originally Posted by Zeev Suraski
    I believe we mentioned once the possibility of adding another error level,
    which is fatal - but still catchable by set_error_handler(). That is a
    good idea (which we should be doing either way).
    Well, lets wait...

  2. #77
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Etnu
    [...] and (if enough people on internals agree with me __dump()).
    I hope they do.
    There’s more than one way to skin a cat.

  3. #78
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello firends

    May someone give more resources about duck typing?

    Thanks in advance

  4. #79
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by abalfazl
    May someone give more resources about duck typing?
    There's really nothing to it: duck typing refers to fact that dynamic languages don't have types, and the only way to know if an object is of a certain type (class, superclass, or interface) is to check its properties. If it works as expected, it is of the required type.

    More on it here: http://en.wikipedia.org/wiki/Duck_typing

  5. #80
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delegate and polymorphism

    Hello firends

    From Microsoft® Visual C#® .NET 2003 Kick Start


    Delegate-Based Polymorphism
    Delegates give you a new form of polymorphism, because you can assign a delegate variable different delegates at runtime. Your code stays the same, but different methods are called depending on which delegates you assign to the delegate variable. You can see an example of this in ch04_15.cs, Listing 4.15, where a single delegate variable, delegateVariable, is assigned two delegates and is used to call the corresponding methods at runtime.

    Listing 4.15 Delegate-Based Polymorphism (ch04_15.cs)
    public class ch04_15
    {
    static public void Main()
    {
    Delegate delegateVariable;
    Messager obj = new Messager();

    delegateVariable = new Delegate(obj.Display1);
    delegateVariable();

    delegateVariable = new Delegate(obj.Display2);
    delegateVariable();
    }
    }

    public class Messager
    {
    public void Display1()
    {
    System.Console.WriteLine("No worries.");
    }

    public void Display2()
    {
    System.Console.WriteLine("No worries again.");
    }
    }

    Here's what you see when you run ch04_15.cs. As you can see, we've used the same delegate variable to call two methods at runtime:



    C:\>ch04_16
    No worries.
    No worries again.
    Is is possible that something like this in PHP?
    http://www.zend.com/zend/php5/php5-delegation.php

    May someone explain about that usage delegate in polymorphism?

    Thanks in advance

  6. #81
    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)
    I don't understand from the example what benefit Delegate is giving you. As the code example stands, I see no benefit to the Delegate at all as opposed to just doing:

    PHP Code:
    $obj->display1();
    $obj->display2(); 
    Perhaps this somehow meant to delay the calling of the method until a later time? If this is the case, you could just make a sort of proxy to the object:

    PHP Code:
    class DisplayProxy {
      protected 
    $obj;
      
    protecter $meth;
      public function 
    __construct($obj$meth) {
        
    $this->obj $obj;
        
    $this->meth $meth;
      }
      public function 
    display() {
        
    $meth $this->meth;
        
    $this->obj->$meth();
      }

    So you could do:
    PHP Code:
     $disp = new DisplayProxy($obj'display1');
    $disp->display();
    $disp = new DisplayProxy($obj'display2');
    $disp->display(); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  7. #82
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basic delegation is simple in php thanks to bug 12622.

    PHP Code:
    class Delegate
    {
       function 
    printFoo() {
          echo 
    $this->foo;
       }
    }

    class 
    {
      var 
    $foo 123;

      function 
    printFoo() {
         
    Delegate::printFoo();
      }
    }

    $p = new P();
    $p->printFoo(); 

  8. #83
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what I've done:
    PHP Code:
    interface IDelegate
    {
        public function 
    invoke($args = array());

    PHP Code:
    class Delegate implements IDelegate
    {
        private 
    $subordinate NULL;
        private 
    $method NULL;
        
        public function 
    __construct($subordinate$method)
        {
            
    $this->subordinate $subordinate;
            
    $this->method $method;
        }
        
        public function 
    invoke($args = array())
        {
            
    $this->subordinate Handle::resolve($this->subordinate);
            
    $callback = array(&$this->subordinate$this->method);
            return 
    call_user_func_array($callback$args);
        }

    PHP Code:
    class Handle
    {
        protected 
    $class NULL;
        protected 
    $args = array();
        
        public function 
    __construct($class$args = array())
        {
            
    $this->class    = (string)    $class;
            
    $this->args        = (array)    $args;
        }
        
        static public function 
    resolve($handle)
        {
            if (
    $handle instanceof self)
            {
                
    $handle call_user_constructor_array($handle->getClass(), $handle->getArgs());
            }
            return 
    $handle;
        }
        
        public function 
    getClass() { return $this->class; }
        public function 
    getArgs() { return $this->args; }

    PHP Code:
    function call_user_constructor_array($class$arguments = array())
    {
        
    $callback = array(new ReflectionClass($class), 'newInstance');
        return 
    call_user_func_array($callback$arguments);

    You can easily build a StaticDelegate etc., and these classes can make for some nice stuff - couple them with an EventHandler object, for instance. Notice how Handle allows lazy loading objects so that they're only created when the Delegate is invoked.

  9. #84
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Basic delegation is simple in php thanks to bug 12622.
    Love the last comment on that bug:

    Quote Originally Posted by Derick
    This is how it is implemented, no bug.
    If something is implemented, it will have no bugs?

    Douglas
    Hello World

  10. #85
    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)
    I agree, it is not a bug because it is the way it is implemented.

    Further, becuase of the way this behaves, we can have PHP Mixins
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  11. #86
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This bug is fixed (this feature is broken, use whatever you want) in php 5. If you call non-static method statically, you'll get E_STRICT warning, if you declare a method as static, there's no $this. Applause.

  12. #87
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    I agree, it is not a bug because it is the way it is implemented.

    Further, becuase of the way this behaves, we can have PHP Mixins
    Well, yes and no. Consider the following example:

    PHP Code:
    class Foo
    {
        public function 
    bar()
        {
            echo 
    $this->yuyu;
        }

        static public function 
    baz()
        {
            echo 
    $this->yuyu;
        }
    }

    class 
    Test
    {
        public 
    $yuyu 'yuyu';

        public function 
    testBar()
        {
            
    Foo::bar();
        }

        public function 
    testBaz()
        {
            
    Foo::baz();
        }
    }

    $t = new Test();

    $t->testBar();
    $t->testBaz(); 
    There are two points:

    First, the example won't work if Test:yuyu is anything but public. Second, the last line also won't work because the Foo::baz() is static.

  13. #88
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, the example won't work if Test:yuyu is anything but public.
    Obviously

    The second example is also what you would expect, since the class method in question is static, it applies locally towards that class method is what I find. You could have for example the following instead I think?

    PHP Code:
    class Foo
    {
        public function 
    bar()
        {
            echo 
    $this->yuyu;
        }

        static public function 
    baz()
        {
            
    // echo $this->yuyu;
    $foo = new Foo();
    echo( 
    $foo -> yuyu ); // but where does yuyu come from huh?
        
    }


  14. #89
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Obviously
    Well, not if you believe Jason and expect it to give you Mixins

    Douglas
    Hello World

  15. #90
    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 sweatje
    I agree, it is not a bug because it is the way it is implemented.
    I think the right question to ask is whether it's documented. If the behavior is not documented, it's not safe to use it. The fact that it's implemented that way and that one developer claims it's supposed to be that way is not convincing enough.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais


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
  •