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

  • Jul. 25, 2014 at 02:28
    Bogus marriages have become an "industry of deceit" that is increasing at an "alarming rate", the Home Affairs select committee warns.
    Read more»
  • Jul. 25, 2014 at 02:26
    Why Indian PM Narendra Modi is avoiding the media
    Read more»
  • Jul. 25, 2014 at 02:22
    French performers march to defend state benefits
    Read more»
  • Jul. 25, 2014 at 02:18
    Arizona halts executions pending a review of its death penalty procedures, after the allegedly botched lethal injection of a convicted murde...
    Read more»
  • Jul. 25, 2014 at 02:14
    Type 2 diabetes is more common in people who work shifts, a large international study suggests.
    Read more»
  • Jul. 25, 2014 at 02:10
    A pioneering breast cancer treatment that replaces weeks of radiotherapy with a single, targeted shot is set to be offered on the NHS.
    Read more»
  • Jul. 25, 2014 at 02:03
    US President Barack Obama has urges lawmakers to end a tax loophole that allows US companies to avoid paying US corporate taxes.
    Read more»
  • Jul. 25, 2014 at 01:24
    At least 13 people have been killed and more than 200 injured when a UN-run school used as a shelter in Gaza came under fire, Gazan health o...
    Read more»
  • Jul. 25, 2014 at 01:22
    Watch the latest news summary from BBC World News. International news updated 24 hours a day.
    Read more»
  • Jul. 25, 2014 at 01:17
    The wreck of a plane that disappeared with 116 people on board on a flight from Burkina Faso to Algiers has been found in Mali, officials sa...
    Read more»
  • Jul. 25, 2014 at 00:18
    Thousands of workers across Ghana take to the streets to protest about the rise in the cost of living and call for petrol subsidies to be re...
    Read more»
  • Jul. 25, 2014 at 00:03
    A US court dismisses a lawsuit against Chiquita brought by Colombians who accuse the banana grower of paying fighters who tortured their rel...
    Read more»
  • Jul. 24, 2014 at 23:49
    Amazon reports a loss of $126m in the second quarter and warns of slowing sales.
    Read more»
  • Jul. 24, 2014 at 23:39
    The first UN aid shipment to Syria delivered without the consent of the government has arrived in the north of the country.
    Read more»
  • Jul. 24, 2014 at 23:35
    The US says Russia has fired artillery at Ukrainian military positions in the east, a week after the Malaysia Airlines flight MH17 disaster....
    Read more»
  • Jul. 24, 2014 at 23:20
    Host nation Scotland has a successful first day at the Glasgow 2014 Commonwealth Games, winning 10 medals.
    Read more»
  • Jul. 24, 2014 at 23:04
    Sir Bradley Wiggins says he will not ride the Tour de France again after describing road racing as "cut-throat".
    Read more»
  • Jul. 24, 2014 at 23:04
    The country where a blasphemy charge is a death sentence
    Read more»
  • Jul. 24, 2014 at 22:42
    The track cyclist from Scotland with an African connection
    Read more»
  • Jul. 24, 2014 at 22:38
    The latest international business headlines from BBC World News.
    Read more»

Recent Comments:

Ehcjntublr

Jun. 11, 2014 at 06:06 PM

leebvfnnbovfmoebzjsbhjkf, http://www.pykbyrhrlo.com/ spdwthctlr

Natural hair care products

May. 27, 2014 at 09:05 AM

These are really great ideas in on the topic of blogging. You have touched some pleasant things here. Any way keep up wrinting.

Remedies for piles

May. 18, 2014 at 05:44 PM

Thanks for finally writing about > %blog_title% < Loved it!

Website

Mar. 25, 2014 at 08:19 PM

There are books, home study courses, workshops and other means of information, to gain the knowledge required to become the alpha leader. If you want ... Read more»

Titia

Feb. 15, 2014 at 01:48 AM

buy car insurance online prozac 40mg no rx best deals on vigra and cialis website auto insurance quoye www.evansvillecommunitytheatre.com ... Read more»

Archives:

Meet me on facebook: