Aggregating objects within PHP5

by , Jun. 26, 2010 at 12:19 PM

Scripts: PHP, MySQL

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

 

  1. <?php
  2. /**
  3.  * Description of Product
  4.  *
  5.  * @author Emmanuel
  6.  */
  7. class Product
  8. {
  9. private $name = null;
  10. private $price = null;
  11.  
  12. public function setProdName($name=null){
  13. $this->name = ucfirst($name);
  14. }
  15. public function getProdName(){
  16. if(isset($this->name)) return $this->name;
  17. else return null;
  18. }
  19. public function setProdPrice($price = null){
  20. if(isset($price))$this->price = $price;
  21. }
  22. public function getProdPrice(){
  23. if(isset($this->price)) return $this->price;
  24. }
  25. }
  26.  
  27. /**
  28.  * Description of Buyer of the product
  29.  *
  30.  */
  31. class Buyer{
  32. private $name = null;
  33.  
  34. public function setBuyername($name){
  35. if(isset($name)) $this->name = $name;
  36. }
  37. public function getBuyerName(){
  38. if(isset($this->name)) return $this->name;
  39. }
  40. }
  41.  
  42. /**
  43.  * Description of ProductCategory
  44.  *
  45.  */
  46. class Manufacturer
  47. {
  48. private $name = null;
  49. protected $product;
  50. protected $buyer;
  51.  
  52. public function __construct(){
  53. $this->product = new Product;
  54. $this->buyer = new Buyer;
  55. }
  56.  
  57. //
  58. public function setName($name){
  59. if(isset($name)){
  60. $this->name = $name;
  61. }
  62. }
  63.  
  64. //
  65. public function getName(){
  66. return $this->name ;
  67. }
  68.  
  69. public function __call($method, $arguments){
  70. if(method_exists($this->product, $method)){
  71. array($this->product, $method),
  72. $arguments
  73. );
  74. }
  75. //
  76. if(method_exists($this->buyer, $method)){
  77. array($this->buyer, $method),
  78. $arguments
  79. );
  80. }
  81. }
  82. }
  83.  
  84. $factory = new Manufacturer();
  85. $factory->setName("FACTORY_1");
  86. //Call methods from aggregated Product object
  87. $factory->setProdName("PRODUCT_1");
  88. $factory->setProdPrice("&euro;150,-");
  89. //Call methods from aggregated Buyer object
  90. $factory->setBuyerName("Emmanuel Manu");
  91.  
  92. echo "Manufacturer's name : ".$factory->getName()."<br/>";
  93. echo "Product's name : ".$factory->getProdName()."<br/>";
  94. echo "Product's price : ".$factory->getProdPrice()."<br/>";
  95. echo "Buyer : ".$factory->getBuyerName()."<br/>";
  96. ?>

 

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

DEMONSTRATION>>

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.

Available 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.

Leave a comment

Required!
Required!

Required!
Required!

Latest News

  • Feb. 27, 2015 at 00:59
    A federal judge in Argentina dismisses accusations that President Cristina Fernandez had covered up alleged Iranian involvement in 1994 a bo...
    Read more»
  • Feb. 27, 2015 at 00:39
    Feyenoord's Europa League game against visitors Roma is suspended after crowd trouble from some home fans.
    Read more»
  • Feb. 27, 2015 at 00:38
    Greek police clash with rioters after the first protest against the left-wing government, ahead of a key German vote on extending the bailou...
    Read more»
  • Feb. 26, 2015 at 23:41
    A $150,000 custom-made Calvin Klein Collection by Francisco Costa dress, worn by actress Lupita Nyong'o at the Oscars, is stolen in Hollywoo...
    Read more»
  • Feb. 26, 2015 at 23:30
    A former aide of Osama Bin Laden has been found guilty of plotting the al-Qaeda bombing of US embassies in east Africa in 1998, killing 224 ...
    Read more»
  • Feb. 26, 2015 at 22:52
    A video released by the Islamic State group appears to show the destruction of statues said to be ancient artefacts from northern Iraq.
    Read more»
  • Feb. 26, 2015 at 22:46
    David Hockney releases artwork showing the landscape that inspired him after it was blighted by fly-tipping.
    Read more»
  • Feb. 26, 2015 at 22:38
    Two llamas went on the lam in Arizona. Thanks to a local news live feed, the US watched - and Tweeted - in rapt attention.
    Read more»
  • Feb. 26, 2015 at 22:28
    The Islamic State militant known as "Jihadi John", who has been pictured in videos of the beheadings of hostages, is named as Mohammed Emwaz...
    Read more»
  • Feb. 26, 2015 at 22:09
    Liverpool are knocked out of the Europa League on penalties by Besiktas in Turkey after their last-32 tie ended 1-1 on aggregate.
    Read more»
  • Feb. 26, 2015 at 21:50
    It may be almost a year before voting begins, but the race for the US presidency is already heating up.
    Read more»
  • Feb. 26, 2015 at 21:32
    House Speaker John Boehner has refused to say if the House will vote on a Senate bill funding homeland security before a Friday deadline.
    Read more»
  • Feb. 26, 2015 at 21:06
    Seven British men who died in the 2013 Algerian hostage crisis were "unlawfully killed", the judge in an inquest into their deaths says.
    Read more»
  • Feb. 26, 2015 at 21:01
    Canada's Supreme Court rejects an appeal to remove the country's citizenship oath, which requires applicants to swear allegiance to the Brit...
    Read more»
  • Feb. 26, 2015 at 20:58
    Suicide bomb attacks kill at least 32 people in northern Nigeria, as the wave of violence from Boko Haram continues.
    Read more»
  • Feb. 26, 2015 at 20:14
    Ghana has secured a $1bn loan from the International Monetary Fund that could stabilise its troubled economy.
    Read more»
  • Feb. 26, 2015 at 19:02
    Afghan rescuers are searching for dozens of people believed to be trapped under snow after a series of avalanches that killed at least 187 p...
    Read more»
  • Feb. 26, 2015 at 19:01
    The US Federal Communications Commission votes in favour of tougher internet regulation, upholding the principle of net neutrality.
    Read more»
  • Feb. 26, 2015 at 19:00
    China imposes a one-year ban on the import of ivory, amid criticism that demand among Chinese consumers is fuelling poaching in Africa.
    Read more»
  • Feb. 26, 2015 at 18:47
    A Belfast man is convicted of assaulting an elderly neighbour by using his belly to "bounce" her away from his home.
    Read more»

Recent Comments:

Emmanuel Ndayiragije

May. 05, 2011 at 04:30 PM

Salut Joannes,
Quand j'ai programmé ce système, je l'ai codé supportant l'englais seulement. Bientôt, je vais l'étendre pour pouvoir supporte... Read more»

Joannes

Apr. 27, 2011 at 02:16 PM

Why don't you write in french?

Administrator

Nov. 12, 2010 at 11:56 AM

The inaccuracy of the Geolocation Api of html5 may be it use the IP from your internet provider or ur pc has no GPS tools installed on it. Geolocation... Read more»

Cr.frozen

Nov. 12, 2010 at 11:33 AM

Html5 geolocation is not accurate?
How come?

Amo

Oct. 20, 2010 at 11:36 PM

HTML5 is great stuff

Archives:

Meet me on facebook: