2
  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);
1
  • 7
    You seem to have conflated two entirely separate things, and I'm not completely certain what the second one is.
    – jonrsharpe
    Commented Nov 13, 2016 at 19:11

2 Answers 2

7
  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.
3
  • 2
    I'd add "needs one and only one and having two will never make sense".
    – gnasher729
    Commented Nov 13, 2016 at 20:42
  • 2
    I wish Tommy Jones and Will Smith could zap all of us to "forget" the Singleton Pattern forever. :-)
    – user949300
    Commented Nov 13, 2016 at 21:57
  • fun times around an application with just one instance of a String....
    – Newtopian
    Commented Nov 14, 2016 at 14:57
1

The singleton pattern does not say "there should be no more than one instance of same class".

The singleton pattern says "if there should be no more than one instance of same class here's a way to achieve that".

The singleton pattern is not a 'best practice'. It's one of many ways to control usage of an object.

Just because the pattern exists does not mean that, for you, it will be a good idea. You have to figure that out yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.