Re: PHP 5.1 - operator overloading

From: Date: Mon, 07 Feb 2005 20:25:10 +0000
Subject: Re: PHP 5.1 - operator overloading
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-14697@lists.php.net to get a copy of this message
(Ditto remark here as the posting I just sent: Please direct any followup privately) >From: "Stanislav Malyshev" <stas@zend.com> > TS>>An advantage of function objects in C++ is that they can be used where > TS>>functions are expected. Nevertheless, there are some features that can be > > Depending on what you mean of "expected". You can't use such object as a > function pointer, for example. Yes, you can write Object() and make that > do Object->operator(). In PHP that won't work, because $object() already > has meaning. Yup, that's one issue. > TS>>used to get something similar, such as create_function(): > > create_function is entirely different thing. It actually dynamically > creates a function (which C++ can't do) which you can call by name. Yes, but they are actually related (although I realise that wasn't clear from my post): Both function objects and dynamically created functions can be used as lambda/unnamed functions, for example for an algorithm. E.g (PHP): usort( $array, create_function('$a, $b', 'return $a->f() < $b->f()')); Yes, we can't create functions on-the-fly in C++ (at runtime), but we have something that can be used in much the same way, and it can also hold state (something you need to use function "static" or some such to do in PHP), function objects, and compile-time function object composition. With the Boost.Lambda library, you may actually do things like: sort(begin, end, _1 < _2); The last part creates a function object on the fly (at compile time), which performs comparison of the elements of the sequence ("begin" and "end" are iterators). > __call > handler is much more like - though not entirely - to the operator() concept. You can say that, but you might also say that it's somewhat similar to create_function(): It "creates" (the impression of a) member function at run time. ;) Regards, Terje

« previous php.internals (#14697) next »