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,
Tag: oop
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
Can I implement an interface and override the return type of one of the interface’s methods?
I have a Task class that extends an abstract class, TaskBase. This TaskBase implements an interface CommanTask, which contains the following method. Now I need a new task class TaskMultiple, and it’s process() method needs to return an array of ProcessResult instead of one ProcessResult. How can I extend abstract class TaskBase for this new TaskMultiple class? Answer If you
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
Transform a average php code to OOP (set_get) [closed]
I have this code I want to Transform to OOP (Set-Get) what the code do is to take some numbers and find the average. function average($array) { foreach ($array as $item) { $total += $…
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
Suggestions on REST API & Software Architecture Style [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 1 year ago. Improve this question First of all i am student, so i need to know how compounds Interact with each other, Maybe next time