Skip to content
Advertisement

Tag: oop

Can you return another instance of the same class from the constructor function (Singleton Pattern)?

I just came across an answer to another question, https://stackoverflow.com/a/32954854/1682790 In that answer, they use the following: But everything else I’ve seen seems to contradict this section of code, namely that in PHP a constructor always returns the object that’s been instantiated, and ignores the return statement altogether. If that’s actually the case, then this isn’t actually creating a singleton,

Composition vs Aggregation in PHP

I was reading an article about relationships in OOP, association, composition, aggregation, etc. Something is confusing & I keep finding conflicting information online so I hope that someone can shed some light on this. So in PHP, we call the following code composition & a lot of articles/tutorials point out to use composition over the inheritance. After reading a few

Laravel Eloquent the same queries

I’ve faced this question on the interview recently. How many objects will be created? How many db queries will be executed? I’ll be thankful so much for any detailed explanation Answer In Either one of the code above, the query will just be 1 $a = Flight::find(1); is same as Since $a & $b are 2 different variables, though they

The correct implementation option is using the magic method __call()

There is a Controller class that contains the magic __call() method, which, when passing an argument, gets a name and creates an instance of the corresponding name. The properties $this of the Controller class are passed to the constructor of this class in order to use the req() function. For example, I recreated the situation from production using examples of

What’s wrong with that simple php class? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed last year. Improve this question Something

Private property in parent class can be accessed and re-assigned through the instance of child class in PHP but protected one can’t

I am very confused why $produk1->harga = 500; can still make a change (or re-assign the value 500) to private $harga property despite private $harga in class Produk has PRIVATE visibility ? $product1 is an instance of class Komik. $produk1 = new Komik(“Naruto”, “Masashi Kishimoto”, “Shonen Jump”, 30000, 100); And by echo $produk1->harga; it prints out 500 and not an

Why PHP asks to pass arguments to the anonymous class?

I am trying to create an anonymous class which extends an abstract class. PHP shows an error: Too few arguments to function class@anonymous::__construct(), 0 passed in TrainerController.php on line 74 and exactly 2 expected I excepted that __construct will not be called, but it seems that it called. I want to create a class, not an object of this class

Advertisement