Object-oriented – Is it good OOP practice to pass object to object and have more than one instance of class

object-orientedpatterns-and-practicesPHP

  1. Singleton pattern saying "there should be no more than one instance of same class", is this something one should stick to when designing PHP OOP applications?
    What are advantages / disadvantages?

    class myDOMDocument {
    
       private $DOMDocument = new DOMDocument();
       private $HTML;
    
       public function __construct($html_str)  {
          $this->HTML = $html_str;
          $this->processHTML();
       }
    
       public function processHTML() {
           ...
       }
    }
    
    //instance 1
    $page_three_obj = new myDOMDocument ($html_one);
    //instance 2
    $page_three_obj = new myDOMDocument ($html_two);
    //instance 3
    $page_three_obj = new myDOMDocument ($html_three);
    
  2. Passing object to an object is good practice in OOP versus passing data between two objects as public method arguments?
    What are advantages / disadvantages of both ways?

Passing object to an object

class one {
  public $myProperty;
  ...
}


$One = new one();

class two {
   private $Obj1;

   public function __construct(One $Obj1) {
      $this->$Obj1 = $Obj1;
   } 

   public function doSomething() {
      ... $this->$Obj1->myProperty ... 
   }
} 

$Two = new two($One);
$Two->doSomething();

vs passing data between two objects

class one {
  public $myProperty;
  ...
}

$One = new one();

class two {

   public function doSomething($inputArg) {
      ... $inputArg ... 
   }

} 

$Two = new two();
$Two->doSomething($One->myProperty);

Best Answer

  1. You seem to be confused. The Singleton pattern says:

"if your application needs one and only one instance of a class, then implement it in a specific way"

and definitely not "there should be no more than one instance of same class" (for each class of your application). The latter one makes seldom sense for any kind of real world OO application. There might be objects from which you need a dozen, thousands or millions of instances, or objects for which only one instance makes sense. It actually depends on the use case and what kind of abstraction you are modeling with your objects.

Independently from this, "Singleton" (as described above, not the misconception of it) is a pattern for which lots of developers nowadays think it is an anti-pattern, but the latter has nothing to do with your initial misunderstanding.

  1. If it is better to pass objects or data when calling methods of another object depends on the kind of abstraction you model with your objects and functions. Sometimes it is better to pass some attributes, sometimes it is better to pass a whole object, sometimes it is better to use helper objects, and there are more lots of variants of that. This cannot be answered with class names like "one" and "two" in a sensible and general manner. Maybe this former question on this site and its answers might help you a little bit.