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 ig…
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 po…
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 …
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 exten…
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 recre…
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 las…
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”, …
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…
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 ea…