ID:               41023
 Comment by:       fqqdk at freemail dot hu
 Reported By:      codeslinger at compsalot dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: any
 PHP Version:      5.2.1
 New Comment:

this stuff should be extended to every member (not only methods, but
properties also)

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

with($obj) {
  $baz = ->foo;
}


Previous Comments:
------------------------------------------------------------------------

[2007-04-08 17:05:15] 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.





------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=41023&edit=1

Reply via email to