php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41023 Enhance PHP "With" a Fluent Interface
Submitted: 2007-04-08 17:05 UTC Modified: 2010-12-22 13:25 UTC
Votes:26
Avg. Score:4.1 ± 1.3
Reproduced:16 of 19 (84.2%)
Same Version:10 (62.5%)
Same OS:13 (81.2%)
From: codeslinger at compsalot dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.1 OS: any
Private report: No CVE-ID: None
 [2007-04-08 17:05 UTC] codeslinger at compsalot dot com
Description:
------------
Given all of the buzz about "Fluent Interface" programming, see for instance: http://devzone.zend.com/node/view/id/1362

I decided that it would be appropriate for me to request the one feature more than any other that I miss not having in the fantastically fabulous PHP.

The "With" operator provides an implied scope for any object Method which does not otherwise specify an explicit object.


The goal is to increase the readability of complex programs which contain repetitive object references (the usual case). It also reduces typing == effort, and potentially enables the compiler to optimize performance/access to the object.  As an additional bonus, maintenance and clarity is increased because there is a central point for the refered to object in a block of code, thus requiring one change instead of many.

I refer you to discussions elsewhere as to the desirability of this syntatic sugar.

1) http://devzone.zend.com/node/view/id/1362
2) http://www.mikenaberezny.com/archives/35
3) http://en.wikipedia.org/wiki/Fluent_interface
4) http://martinfowler.com/bliki/FluentInterface.html
5) http://schlitt.info/applications/blog/index.php?/archives/400-A-sensible-place-for-a-fluent-interface.html



Reproduce code:
---------------
---------------
Thus:

$oMyFoo = new ClassFoo;

$oMyFoo->DoSomething();
$oMyFoo->DoSomethingElse();
$oMyFoo->DoOther();
$oMyFoo->DoEtc();

$Assigned = $oMyFoo->DoSomethingElse() + $oMyFoo->SomeOther() + $oMyFoo->Another();


Expected result:
----------------
-------------------------------------------------
Under the "Fluent Interface" paradigm in which every Method returns a reference to *the* object, it becomes:

$oMyFoo = new ClassFoo;

$oMyFoo->DoSomething()
  ->DoSomethingElse()
  ->DoOther()
  ->DoEtc()
;

Note that the above is really just:

$oMyFoo->DoSomething()->DoSomethingElse()->DoOther()->DoEtc();


However, the object class must be specially written to support this and you can not return other values, thus the "$Assigned =" is not possible.

But by implementing a "With" construct, any object can be used without modification, and you do not give up the ability to return values.

-----------------
Desired Approach:

$oMyFoo = new ClassFoo;

With($oMyFoo)
{ 
  ->DoSomething();
  ->DoSomethingElse();
  ->DoOther();
  ->DoEtc();

  $Assigned = ->DoSomethingElse() + ->SomeOther() + ->Another();
}


For any unqualified Method, the referent object is defined by the "With" clause.  Naturally, "With" can be nested.

As you can see, the readability of the code is greatly enhanced and full functionality is retained.




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-01 13:15 UTC] fqqdk at freemail dot hu
this stuff should be extended to every member (not only methods, but properties also)

$obj = new stdClass;
$obj->foo = 'bar';

with($obj) {
  $baz = ->foo;
}
 [2009-01-08 20:30 UTC] akimmalkov at gmail dot com
It's usefull ability and I often need it when coding.
 [2010-12-22 13:25 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-12-22 13:25 UTC] johannes@php.net
See #39340
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 00:01:29 2024 UTC