From:             codeslinger at compsalot dot com
Operating system: any
PHP version:      5.2.1
PHP Bug Type:     Feature/Change Request
Bug description:  Enhance PHP "With" a Fluent Interface

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 bug report at http://bugs.php.net/?id=41023&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=41023&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=41023&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=41023&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=41023&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=41023&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=41023&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=41023&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=41023&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=41023&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=41023&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=41023&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=41023&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=41023&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=41023&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=41023&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=41023&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=41023&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=41023&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=41023&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=41023&r=mysqlcfg

Reply via email to