Aggregating objects within PHP5
by Emmanuel Ndayiragije, Jun. 26, 2010 at 12:19 PM
If you did not know, it is worth to know that from PHP5 the full (OOP) Object Oriented programming paradigm is totally integrated.
In this article, i am going talk about one of the technics of OOP with PHP. If you do not have any notion in Object oriented programming, in any programming languages like Java, C++, C#, Ruby, ...
i will advise to first learn OOP paradigm or find some introduction to Object oriented programming on the internet. You may purchase some book on this matter, if you want.
When you go OOP programming, you will in one or other way interact with some objects. Thus aggregation of object refers to a situation where one or more objects( the Aggregated objects ) are passed to another object( the Aggregating object ) to perform some operations or methods within that object that called it.
The aggregating object instatiates or creates an object of the aggregated class in its constructor method. To be able for the aggregating object calling methods or public properties of the aggragated objects, the PHP5 magic method (__call) come in handful. Within this __call method, another PHP function (call_user_func_array) comes in handy for matching the aggregated object to its called method.
May be it is not easy to figure out how it is all about. But in the classes defined here underneath, you can get it. You can also check the demonstration of this code
<?php /** * Description of Product * * @author Emmanuel */ class Product { private $name = null; private $price = null; public function setProdName($name=null){ } public function getProdName(){ else return null; } public function setProdPrice($price = null){ } public function getProdPrice(){ } } /** * Description of Buyer of the product * */ class Buyer{ private $name = null; public function setBuyername($name){ } public function getBuyerName(){ } } /** * Description of ProductCategory * */ class Manufacturer { private $name = null; protected $product; protected $buyer; public function __construct(){ $this->product = new Product; $this->buyer = new Buyer; } // public function setName($name){ $this->name = $name; } } // public function getName(){ return $this->name ; } public function __call($method, $arguments){ $arguments ); } // $arguments ); } } } $factory = new Manufacturer(); $factory->setName("FACTORY_1"); //Call methods from aggregated Product object $factory->setProdName("PRODUCT_1"); $factory->setProdPrice("€150,-"); //Call methods from aggregated Buyer object $factory->setBuyerName("Emmanuel Manu"); echo "Manufacturer's name : ".$factory->getName()."<br/>"; echo "Product's name : ".$factory->getProdName()."<br/>"; echo "Product's price : ".$factory->getProdPrice()."<br/>"; echo "Buyer : ".$factory->getBuyerName()."<br/>"; ?>
If you would want to see the code of the classes above in action, go to this link for demonstration. The echo's in the code will print up the name of the factory, product's name and its price, the name of the buyer
If we talk about performance issues, the advantages of aggregating objects within other objects mainly come from its lower overload, since most of the time only one object is shared by other objects. However, this advantage might be discarded in the case of having a class for database connection shared by other multiple classes. You may run into difficulties if multiple database connections are established to the same server, causing a noticeable detriment to the system, particularly if your site is attracting many visitors.
Of course it is not a good idea to design your DB object with Objects Aggregation, instead the Singleton Pattern may be in this case, providing one and only existing object for DB, very useful. This is to avoid creating new instances of a DB class with key (new) and end up with overheading the system beacause every instantiated object is kept in the memory of the system.
Patterns in PHP is also very important to effectively and efficiently design not only webapplications but also any soft. applications. Patterns topics will come in the latter articles.
Post Comment|
Permant linksAvailable Comments :
Your Attention! Please do not post message with spam on this blog. We try to be cool to each other and post relevant comments and relating to the appropriated topic or entry. Let's try to get a nice conversation over here.
Entry Categories:
Latest News
- Jun. 19, 2013 at 06:43
The UN says 7.6 million people became refugees in 2012, the highest number since 1994, with the conflict in Syria a major new factor.
Read more» - Jun. 19, 2013 at 06:37
Riot police and demonstrators clash in new protests in Brazil's largest city, Sao Paulo, after President Rousseff praises the protests' aims...
Read more» - Jun. 19, 2013 at 06:13
Military helicopters lead rescue operations in India's flood-hit northern states, where 130 people are now known to have died.
Read more» - Jun. 19, 2013 at 06:08
The Cassini probe in orbit around Saturn is going to picture the ringed planet in a special photo that also includes a distant Earth.
Read more» - Jun. 19, 2013 at 06:01
England's healthcare regulator may have covered up knowledge of its own failings after a series of baby deaths at a Cumbria hospital, a repo...
Read more» - Jun. 19, 2013 at 06:00
The IMF is turning into a university
Read more» - Jun. 19, 2013 at 05:34
Australian Adrian Bayley is jailed for the rape and murder of Irish woman Jill Meagher, with a minimum jail term of 35 years.
Read more» - Jun. 19, 2013 at 05:22
The Republican-controlled US House of Representatives passes a bill to restrict abortions to the first 20 weeks after conception.
Read more» - Jun. 19, 2013 at 05:11
Fears for author of 'I'm Queer' blogpost
Read more» - Jun. 19, 2013 at 04:38
A hairy crab named after US actor David Hasselhoff hitched a ride on an ocean "super-highway" to colonise deep sea vents in the Atlantic ten...
Read more» - Jun. 19, 2013 at 03:40
An open letter published in the Lancet medical journal calls for hunger-striking Guantanamo Bay detainees to receive independent medical car...
Read more» - Jun. 19, 2013 at 03:36
US President Barack Obama arrives in Berlin at the start of a visit during which he will address crowds at the city's famous Brandenburg Gat...
Read more» - Jun. 19, 2013 at 03:14
Is down-at-heel Detroit on the brink of a comeback?
Read more» - Jun. 19, 2013 at 03:13
Watch the latest news summary from BBC World News. International news updated 24 hours a day.
Read more» - Jun. 19, 2013 at 02:52
Supreme Court judges are to decide whether relatives of British soldiers killed in Iraq can sue the government for damages under human right...
Read more» - Jun. 19, 2013 at 02:50
The head of the US electronic spying agency tells Congress surveillance programmes leaked to media helped thwart 50 attacks since 2001.
Read more» - Jun. 19, 2013 at 02:46
Four US soldiers have been killed at Bagram airbase in Afghanistan, hours after the US announced direct talks with the Taliban.
Read more» - Jun. 19, 2013 at 02:30
A report commissioned by the UK government calls for new measures, including criminal sanctions, to make bankers responsible for their own f...
Read more» - Jun. 19, 2013 at 02:23
Suspected Boko Haram militants in Nigeria kill at least nine school children, in what survivors suggest is a response to growing vigilante g...
Read more» - Jun. 19, 2013 at 02:04
The quality of a performance does not drive the amount of applause an audience gives, a study suggests.
Read more»
Recent Comments:
May. 07, 2013 at 10:57 AM
Pleasing to find soemone who can think like that
May. 07, 2013 at 10:51 AM
Thinking like that is really amaizng
Oct. 27, 2012 at 03:57 PM
That framework (Kazinduzi) is right good, & simple to use, i download it and set it on my ubuntu box, it just works fine. good work, man.
Oct. 27, 2012 at 03:56 PM
Great framework, it works fine on my server.
Jun. 29, 2012 at 05:27 PM
Reaction to "Cheap online prescription glasses".
Thanks for your appreciation to my blog.
Of course this blog is about to publish my own expe... Read more»
Archives:
- Playing video and audio with HTML5 July 23, 2010
- Aggregating objects within PHP5 June 26, 2010
- Creating a login system in your website May 05, 2010
- How to Setup your own Webserver May 05, 2010
- Welcome Entry May 05, 2010





