You're probably refering to this threadOriginally Posted by Etnu
http://groups.google.com/group/maili...f2b34cd7c85d02
Well, lets wait...Originally Posted by Zeev Suraski
| SitePoint Sponsor |





You're probably refering to this threadOriginally Posted by Etnu
http://groups.google.com/group/maili...f2b34cd7c85d02
Well, lets wait...Originally Posted by Zeev Suraski


I hope they do.Originally Posted by Etnu
![]()
There’s more than one way to skin a cat.




Hello firends
May someone give more resources about duck typing?
Thanks in advance




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.Originally Posted by abalfazl
More on it here: http://en.wikipedia.org/wiki/Duck_typing




Hello firends
From Microsoft® Visual C#® .NET 2003 Kick Start
Is is possible that something like this in PHP?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.
http://www.zend.com/zend/php5/php5-delegation.php
May someone explain about that usage delegate in polymorphism?
Thanks in advance
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:
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:$obj->display1();
$obj->display2();
So you could do: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();
}
}
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.





Basic delegation is simple in php thanks to bug 12622.
PHP Code:class Delegate
{
function printFoo() {
echo $this->foo;
}
}
class P {
var $foo = 123;
function printFoo() {
Delegate::printFoo();
}
}
$p = new P();
$p->printFoo();




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; }
}
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.PHP Code:function call_user_constructor_array($class, $arguments = array())
{
$callback = array(new ReflectionClass($class), 'newInstance');
return call_user_func_array($callback, $arguments);
}





Love the last comment on that bug:Originally Posted by stereofrog
If something is implemented, it will have no bugs?Originally Posted by Derick
Douglas
Hello World
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.





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.




Well, yes and no. Consider the following example:Originally Posted by sweatje
There are two points: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();
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.





ObviouslyFirst, the example won't work if Test:yuyu is anything but public.
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?
}
}





Well, not if you believe Jason and expect it to give you MixinsOriginally Posted by Dr Livingston
Douglas
Hello World




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.Originally Posted by sweatje
Dagfinn Reiersøl
PHP in Action / Blog / Twitter
"Making the impossible possible, the possible easy,
and the easy elegant" -- Moshe Feldenkrais
Bookmarks