473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Chaining in OOP

150 100+
while i try to learn Zend framework i saw this code:
Expand|Select|Wrap|Line Numbers
  1. $this->addDecorator('FormElements')->addDecorator('HtmlTag', array('tag' => 'dl'))->addDecorator('Fieldset')->addDecorator('DtDdWrapper');
I'm new in OOP, i need an example to do something like that.
Jan 11 '09 #1
3 1416
Markus
6,050 Expert 4TB
Can you clarify your question please?
Jan 11 '09 #2
pbmods
5,821 Expert 4TB
Heya, Smartic.

You're probably talking about chaining (which falls under a programming style known as Fluent Interface).

When you want to implement chaining, you simply need to make sure that each method call returns a reference to its parent instance by using the $this keyword:

Expand|Select|Wrap|Line Numbers
  1. class Foo
  2. {
  3.   public function bar(  )
  4.   {
  5.     return $this;
  6.   }
  7.  
  8.   public function baz(  )
  9.   {
  10.     return $this;
  11.   }
  12. }
  13.  
  14. $Foo = new Foo();
  15. $Foo->bar()->baz();
  16.  
PHP does not allow you to chain off of the constructor, so you have to initialize (in this case) the Foo instance in a separate statement.

Note that $Foo->bar() returns a reference to $Foo, which allows you to chain the subsequent call to baz(). It would be equivalent to writing this:

Expand|Select|Wrap|Line Numbers
  1. $Foo = new Foo();
  2. $Foo->bar();
  3. $Foo->baz();
  4.  
Jan 12 '09 #3
smartic
150 100+
thank you pbmods
Jan 12 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: danishce | last post by:
I want to encrypt the ASCII message using Cipher Block Chaining MAC and generate the 8 byte key.How can i achieve this? Thanks: Danish Majid
0
by: dsithoo | last post by:
Hi, I have a stored procedure on database A (owner dbo) which is itself owned by dbo. It tries to do a SELECT from a table in database B (both table and db owned by dbo) but fails with: "Msg...
1
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an...
3
by: DougieC | last post by:
Hello all, I am still relatively new to MySQL and databases in general. I am working on populating a mastertable for Human Resource and have developed a series of MySQL commands to accomplish...
0
by: ramadeviirrigireddy | last post by:
Can anyone tell me what is the difference between Servlet Chaining and Servlet Forward ? thanks in advance
9
realin
by: realin | last post by:
hi guys, well this time i need to ask something related to database and php. I want to make certain insert statements in multiple tables and i want that to do that a more consistent way. Just...
7
by: FireImp | last post by:
So I've read a lot of post about how to run a batch file from within the C# program. And I followed the instructions with a few alterations to fit my needs. First I am using cmd.exe to actually run...
8
by: escher4096 | last post by:
I have a class A that is inheriting from class B. Class B has a constructor that I would like to use in Class A, so I chain to it. However class B's constructor often misbehaves and it causes a...
3
by: KLonergan | last post by:
Hello everyone, I'm trying to recreate the C# dictionary class using binary trees for chaining. I have my binary tree class complete, and all I really need now is to connect my array with numerous...
16
dmjpro
by: dmjpro | last post by:
Hey Experts! I want to know the prototype chaining? How does it take part in Inheritance in JavaScript?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.