# new feature -> with()

- Thread: https://externals.io/message/32721
- Original archive: https://news-web.php.net/php.internals/32721
- Started by Sebastian on 2007-10-09T19:12:05+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #32721 — Sebastian — 2007-10-09T19:12:05+00:00

URL: https://externals.io/message/32721#32721

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

---

## #32722 — Richard Black — 2007-10-10T14:04:10+00:00

URL: https://externals.io/message/32721#32722 · In reply to: #32721

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

---

## #32726 — Ralph Schindler — 2007-10-10T16:08:52+00:00

URL: https://externals.io/message/32721#32726 · In reply to: #32721

> ---------------------------
> $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

---

## #32740 — Sebastian — 2007-10-12T20:28:02+00:00

URL: https://externals.io/message/32721#32740 · In reply to: #32721

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()`


>
> > […]
>
> 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

---

## #32846 — Hartmut Holzgraefe — 2007-10-18T07:54:39+00:00

URL: https://externals.io/message/32721#32846 · In reply to: #32740

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
