On Tue, Mar 18, 2008 at 10:49 PM, Ray Hauge <[EMAIL PROTECTED]>
wrote:

> Nathan Nobbe wrote:
> > if we were going to see support for anything anonymous that i would
> > welcome it would be anonymous objects and the ability to create an
> > object on the fly from an interface as per java 5.
>
> I'm not sure if this would solve your problem (my lack of java knowledge
> is showing), but you can create objects either by creating a new
> stdClass() object, or by using (object)NULL.  Example:
>

ive messed around w/ stdClass and use it more frequently than most i
imagine, but mostly just as a data transport mechanism.  sometimes i prefer
it over an array, what can i say i like the -> ;)  but seriously, using it
as an anonymous object doesnt really work.  there are some posts on
php.netabout this actually (search for anonymous object)
http://www.php.net/zend-engine-2.php


> <?php
>
> $object = (object) NULL;
>
> $object->test1 = 1;
> $object->test2 = 2;
>
> echo $object->test1 . "\n";
>
> echo $object->test2;
>
> ?>
>
> After trying to add a way to call a function from an object variable, I
> have come to the conclusion that it's kinda ugly.
>
> <?php
>
> $obj = (object) NULL;
>
> $obj->test = 2;
> $obj->myTestFunc = "myTestFunc";
>
> function myTestFunc () {
>    return "test";
> }
>
> echo call_user_func($obj->myTestFunc) . $obj->test;
>
> ?>


ive tried experimenting w/ making php more functional, attempting to attach
methods in one way or another to an object, but alas, my attempts have all
been quite fleeting :(


> Does anyone have a link to some documentation about the stdClass?  All I
> could find was a bunch of bug reports and other stuff that wasn't what I
> was looking for.  I would have thought there'd be a page for it in the
> manual, but I didn't find one there either.


php --rc stdClass

thats about all there is :)  but it is nice for usage in conjunction w/
json_encode().

and just to give you an idea what you can do in java (note nothing like
javascript which is why i think its applicable to php [because its already
similar to java])
suppose you have a class,

class N {
  function b() { echo 'b'; }
}

you can create an anonymous sub class like this (added twisted php notation
to give good visual of what could be possible)

$n = new N() {
  function b() { echo "b's been overriden"; }
};

and you can do the same w/ an interface as well, just swap class by
interface in the definition of N above.  you can even do this when supplying
an actual parameter to a method call, something like

function callBOnN(N $n) { $n->b(); }

now suppose you dont have an implementation of N, suppose its an interface
or you dont like the base N class (if it were a class) or you like it you
just dont have an instance of it or whatever, you could do this,

callBOnN(new N() {
  function b() { echo 'defined this b on the fly!'; }
});

thats some cool stuff and personally i would much rather see php take this
route than mess around w/ closures.  it seems much more natural to me (as
far as the future direction of php).  but then again i feel like im way more
into oop than most people on the list let alone the php community as a whole
so likely im hopelessly outnumbered.

-nathan

Reply via email to