new feature -> with()

php.internals

Sebastian

18 years ago
hi, i think it would be really handy to introduce the with() feature from JavaScript (and probably other OOP languages) into php. so for sample the following --------------------------- $class=new class; $class->do_something(); $class->do_more(); $class->do(); --------------------------- could be made easier and more readable: --------------------------- $class=new class; with($class) { do_something(); do_more(); do(); } --------------------------- greetings Sebastian

Unnamed Person

18 years ago
I'm usually a lurker on here, but thought I'd through in my 2p on this one... Delphi (Object Pascal) has a similar feature, that I've had some experience of using, and I never liked it. Why? Because it only leads to confusion, mainly because the separation of object and method leaves you unsure of what is actually being called. Consider the following: function do() { echo "hello"; } $class=new class; with($class) { do_something(); do_more(); do(); } Now, within my with(), am I calling the class method do() or the function do()? Not clear at first glance. And the whole thing gets even more complex if you can start nesting with() constructs. I suspect this wouldn't be trivial to implement, and for me it doesn't add any value - only potential confusion. ========================================== Richard Black - Senior Consultant DataVisibility Ltd Tel. 020 7917 9570 http://www.datavisibility.com/ ------------------------------------------------------------------------ -------------------- Registered Office: 212 Piccadilly, London, W1J 9HF Registered in England No. 5891154 VAT No. 8877891834 This document should only be read by those persons to whom it is addressed. Its contents are private and confidential. If you receive this email in error, please notify the sender immediately and do not disclose, copy or distribute this message, or open any attachments. -----Original Message----- From: Sebastian [mailto:sebastian@ifyouwantblood.de] Sent: 09 October 2007 20:12 To: internals@lists.php.net Subject: [PHP-DEV] new feature -> with() hi, i think it would be really handy to introduce the with() feature from JavaScript (and probably other OOP languages) into php. so for sample the following --------------------------- $class=new class; $class->do_something(); $class->do_more(); $class->do(); --------------------------- could be made easier and more readable: --------------------------- $class=new class; with($class) { do_something(); do_more(); do(); } --------------------------- greetings Sebastian
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Hartmut Holzgraefe

18 years ago
Richard Black wrote:
> [...] > for me it doesn't add any value - only potential confusion.
+0.9 it makes sense as a convenience tool if used correctly but can create a hell of a maintenance nightmare if not Almost as bad as GOTO maintenance wise but without the benefits in edge cases. When reading foreign C++ code a lot you learn to love the explicit nature of PHP naming scopes even though it is extra typing as it is clear at first sight whether you are looking at a variable, member variable, function, object method ... It is also clear that everything is from a local scope unless explicitly declared as global within that local scope ... Lets do not weaken this concept ...
-- Hartmut Holzgraefe, Principal Support Engineer . Discover new MySQL Monitoring & Advisory features at: http://www.mysql.com/products/enterprise/whats_new.html Hauptsitz: MySQL GmbH, Dachauer Str.37, 80335 München Geschäftsführer: Kaj Arnö - HRB München 162140

T.R. Cox

18 years ago
Hartmut Holzgraefe wrote:
> When reading foreign C++ code a lot you learn to love > the explicit nature of PHP naming scopes even though > it is extra typing as it is clear at first sight whether > you are looking at a variable, member variable, function, > object method ... > > .... > > Lets do not weaken this concept ...
I too normally lurk and read, but having played with other languages I've come to appreciate the immediate clarity of the explicit nature of PHP. Therefore, I agree completely. I'd rather not see this added to the language.

Stut

18 years ago
Richard Black wrote:
> I'm usually a lurker on here, but thought I'd through in my 2p on this > one... > > Delphi (Object Pascal) has a similar feature, that I've had some > experience of using, and I never liked it. Why? Because it only leads to > confusion, mainly because the separation of object and method leaves you > unsure of what is actually being called. > > Consider the following: > > function do() > { > echo "hello"; > } > > $class=new class; > > with($class) > { > do_something(); > do_more(); > do(); > } > > Now, within my with(), am I calling the class method do() or the > function do()? Not clear at first glance.
It may not be clear to some people, but there are many things in the every language that aren't necessarily obvious and I don't think that's a reason not to implement something. To me this is actually quite clear. When you use with() you are defining a temporary local scope, so your call to do() would first check if the with() object contains that method, and then check the global scope if not.
> And the whole thing gets even more complex if you can start nesting > with() constructs.
Again this seems simple to me, you just need a stack of with() objects which are walked up to resolve any references.
> I suspect this wouldn't be trivial to implement, and for me it doesn't > add any value - only potential confusion.
I see great value in this - it makes for cleaner looking code. I need more than 10 fingers and 10 toes to count how many source files I have that contain long sections where each line contains multiple references to the same object. -Stut
-- http://stut.net/

Rasmus Lerdorf

18 years ago
Stut wrote:
> It may not be clear to some people, but there are many things in the > every language that aren't necessarily obvious and I don't think that's > a reason not to implement something.
I think that is one of the strongest reasons not to implement something actually. If there is a way to do something in a clear and concise syntax, adding an alternate less clear syntax that isn't immediately obvious to everyone simply obfuscates the language. The other fatal blow to a feature like this is that it would be quite a bit slower to compile and execute than the longer form because we now can't simply do a single hash lookup in the function table when we see a function. We first have to walk up the tree of objects to see if there is a method with this name, and if there is no method, then we assume it is a straight function call, unless of course there is a __call() method, then what? Way too many gotchas here. -Rasmus

Stut

18 years ago
Rasmus Lerdorf wrote:
> Stut wrote: >> It may not be clear to some people, but there are many things in the >> every language that aren't necessarily obvious and I don't think that's >> a reason not to implement something. > > I think that is one of the strongest reasons not to implement something > actually. If there is a way to do something in a clear and concise > syntax, adding an alternate less clear syntax that isn't immediately > obvious to everyone simply obfuscates the language.
Fair point, although as I stated this one is obvious to me.
> The other fatal blow to a feature like this is that it would be quite a > bit slower to compile and execute than the longer form because we now > can't simply do a single hash lookup in the function table when we see a > function. We first have to walk up the tree of objects to see if there > is a method with this name, and if there is no method, then we assume it > is a straight function call, unless of course there is a __call() > method, then what? Way too many gotchas here.
Hadn't considered __call() possibilities, and I didn't think the performance hit would be too great but I'm the first to admin I don't know the innards of PHP particularly well yet. -Stut
-- http://stut.net/

Keryx Web

18 years ago
Rasmus Lerdorf skrev:
> I think that is one of the strongest reasons not to implement something > actually. If there is a way to do something in a clear and concise > syntax, adding an alternate less clear syntax that isn't immediately > obvious to everyone simply obfuscates the language. > > The other fatal blow to a feature like this is that it would be quite a > bit slower to compile and execute than the longer form because we now > can't simply do a single hash lookup in the function table when we see a > function. We first have to walk up the tree of objects to see if there > is a method with this name, and if there is no method, then we assume it > is a straight function call, unless of course there is a __call() > method, then what? Way too many gotchas here. >
May I also add that the use of "with" is generally considered bad practice by leading JavaScript gurus like Douglas Crockford, David Flanagan, John Resig and PPK. I even have a vague memory of Brendan Eich commenting upon the feature negatively as well. Crockford speaks at: http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/ Pro s& Cons are listed at: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:with Erik Arvidsson warns against the use in JS at: http://erik.eae.net/archives/2005/04/11/18.39.51/ Lars Gunther

Ralph Schindler

18 years ago
> --------------------------- > $class=new class; > > with($class) > { > do_something(); > do_more(); > do(); > }
What more value does this hold over implementing fluent interfaces which are already possible? Assuming class Foo { public function doSomething() { /**code**/ return $this; } public function doMore() { /**code**/ return $this; } public function doDo() { /**code**/ return $this; } } You can do this: $bar = new Foo() $bar->doSomething()->doMore()->doDo(); or, if you want exceptional fallthrough: try { $bar->doSomething() ->doMore() ->doDo(); } catch (Exception $e) { ... } -ralph

Sebastian

18 years ago
hi, wow, thats what you're writing is a chain call. this only works, if the methods of a class return the object $this. but if you need a return value other than $this, simply there is no real way. and moreover i don't agree with the other arguments. what i'm suggesting is, that in with(){} you are only allowed to use the methods and variables defined in the object you are using with(). as in the sample, you are not using any global functions or else. my opinion is, this it is especially with $this very useful. greetings ----- Original Message ----- From: "Ralph Schindler" <ralph@smashlabs.com> To: "Sebastian" <sebastian@ifyouwantblood.de> Cc: <internals@lists.php.net> Sent: Wednesday, October 10, 2007 6:08 PM Subject: Re: [PHP-DEV] new feature -> with()

Hartmut Holzgraefe

18 years ago
Sebastian wrote:
> [...] what i'm suggesting is, > that in with(){} you are only allowed to use the methods and variables > defined in the object you are using with(). as in the sample, you are not > using any global functions or else.
this would render 'with' mostly useless as in most real world situations where a 'with' block might make sense it wouldn't be standalone but would at least require some basic function calls to be of use ... so with this limitation in place i'd like to revert my previous -0.9 to a strong -1
-- Hartmut Holzgraefe, Principal Support Engineer . Discover new MySQL Monitoring & Advisory features at: http://www.mysql.com/products/enterprise/whats_new.html Hauptsitz: MySQL GmbH, Dachauer Str.37, 80335 München Geschäftsführer: Kaj Arnö - HRB München 162140