[nycphp-talk] Intellectual Monday
Rob Marscher
rmarscher at beaffinitive.com
Mon Jan 22 15:37:51 EST 2007
A lot of the methods in the Zend Framework classes use object chaining.
It requires PHP5. In terms of the prototype concept... javascript
doesn't have a traditional object implementation -- you can't say "class
A extends class B" -- and the lines are pretty blurred between what a
function is and what an object is, so that why you kind of have to hack
it via the prototype object to achieve some sense of object-oriented
javascript.
Here's the idea of object chaining in php:
class SomeClass
{
function doSomething()
{
echo "Something\n";
return $this;
}
function getOther()
{
if (!isset($this->other))
{
$this->other = new OtherClass();
}
return $this->other;
}
}
class OtherClass
{
function doSomethingElse()
{
echo "Something Else\n";
return $this;
}
}
$foo = new SomeClass();
$foo->doSomething()->getOther()->doSomethingElse();
-Rob
csnyder wrote:
> On 1/22/07, Cliff Hirsch <cliff at pinestream.com> wrote:
>
>> I wonder how these techniques work in the PHP world. Does anyone use
>> chaining effectively? Is there a PHP equivalent to the prototype
>> concept?
>
> Javascript is an beautiful, powerful language. I never thought I would
> say that, and I think Netscape could have done so much more "back in
> the day" to produce good documentation and examples. That's what
> Prototype, MochiKit, jQuery, et al are finally doing.
>
> Anyway, PHP's object support doesn't really compare. Classes are not
> the same as prototypes. And not everything in PHP is an object,
> therefore chaining is a kludge at best.
>
>> From the point of view of readability and maintainability, I hope
> developers *aren't* trying to replicate Javascript patterns in PHP...
>
More information about the talk
mailing list