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
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
--
To unsubscribe,
visit: http://www.php.net/unsub.php
$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
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()
$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 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