Issue with call function not operating consistently across all subclasses

I am experimenting with using the __call function for pre-action instructions.

The issue I am facing is that the __call function only works once, but I need it to be triggered every time the run() method is called.

When I call $second_child->run() inside the run() method of the first child class, only the __call function of the first child is executed. Any insights into why this happens?

class ParentClass{
    protected function run(){}
    public function __call($method,$arguments) {
        echo "<br/>######Call ". get_called_class()."########<br>";
        if(method_exists($this, $method)) {
            call_user_func_array(array($this,$method),$arguments);
        }
    }
}
class first_child extends ParentClass{
    protected function run(){
        echo "<br>running " . get_called_class();
        $x= new second_child;
        $x->run();
    }
}
class second_child extends ParentClass{
    protected function run(){
        echo "<br>running " . get_called_class();
    }
}
$y= new first_child;
$y->run();

Answer №1

This occurs due to the fact that second_child is an extension of ParentClass, which includes the run method.

As a result, first_child has the ability to invoke the (overridden) protected method in second_child, thus bypassing the magic method that is only executed when direct method calling is not possible.

In this scenario, there exists a direct method calling possibility because first_child inherits access to the protected methods from ParentClass, and PHP utilizes polymorphism to call the overridden method within second_child.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I effectively iterate through an array in PHP while considering an offset?

array( (int) 0 => '3', (int) 1 => '5', (int) 2 => '9', (int) 3 => '14', (int) 4 => '16', (int) 5 => '17', (int) 6 => '18', (int) 7 => '19', (int) 8 => ...

Unexpected server error encountered during navigation of php pages

I am using PHP to retrieve data from a MySQL table and display it in an HTML table. Each row in the table has a 'remove' button that triggers a PHP script (remove.php) to delete the corresponding row from the database table and return to admin.ph ...

Guide to extracting specific elements from JSON using PHP

After retrieving a JSON from a URL, I am attempting to extract information using PHP. Here is what I have so far: $url = 'https://www.anwb.nl/feeds/gethf'; $result = file_get_contents($url); $array = json_decode($result, TRUE); echo $array[1][ ...

Cross-origin resource sharing (CORS): In PHP, the response to the preflight request is not successfully passing. I am permitting

With the abundance of CORS posts already out there, I find myself adding to them in search of a solution. My dilemma involves building an angular 4 application that interacts with my php api. Locally, everything works seamlessly. However, once I upload the ...

Cannot access Codeigniter subfolder

I am facing an issue with codeigniter htaccess and need some assistance. My demo site URL is sampleurl.com and all my files are located in a subfolder named demo. -sampleurl.com -demo - application - system - index.php -demo1 I hav ...

What is the best way to send a JSON string from a Corona SDK app to a PHP server?

I'm in the process of building an app with Corona SDK. I'm facing an issue where I am sending null values to a mySQL server even though the variable on the app side has a value in the form of a json string. The php code works fine for entering da ...

Get rid of the .php extension in the URL completely

Lately, I've been experimenting a lot with the .php extension. I successfully used mod_rewrite (via .htaccess) to redirect from www.example.com/example.php to www.exmaple.com/example. Everything is running smoothly. However, I noticed that even though ...

"Incorporating a new column into the database through the model and controller in Yii

When working in the Yii MVC framework, I decided to add a new column to my database model using the following code: $success = Yii::app()->ft_website_prod->createCommand()->addColumn('ft_website.users', 'columnname', 'va ...

Are there any reliable PHP classes available to generate HTML tables efficiently?

Searching for an advanced PHP class that can effortlessly create intricate HTML tables, including the ability to handle colspan/rowspan and assign specific CSS classes to rows, columns, and cells. ...

Find all characters that are not letters using preg_match

Is there a quicker way to represent the opposite of \p{L}? I am looking to find a match for everything except letters like a-zA-ZæøåÆØÅ ... One option is using [^\p{L}], but is there a more concise method available? ...

Avoiding the ability to repeatedly click the "add to cart" button in WordPress/WooCommerce

I am looking for a solution to prevent double clicking on the add to cart button in WordPress. Currently, I can click on the button multiple times and it causes issues with the product quantity in the cart page. The cart page incorrectly shows that the pro ...

What is the best way to reveal a message with a fading effect when the value is true on a GET

Is there a way to pass a value to a PHP page using GET and then trigger a jQuery function based on the value being true? I'm currently experimenting with... <script type="text/javascript" language="javascript"> function showError(){ $(&apo ...

Evade Incapsula's JStest

Seeking assistance with updating my status using cURL on a website secured by Incapsula. I am encountering difficulty accessing the main page due to their JS test security measures, despite cloning headers, useragent, and IP. Can anyone suggest a solution ...

Creating a zip file from database or binary file content using PHP

I'm looking to create a zip file using PHP, however my source files are all stored in databases rather than on disk. The content of the files I need to pack is saved as binary data in one database, while the metadata is stored in another. I've ...

Exploring the depths of Laravel through debugging techniques using PHP, AJAX, and the

Can you recommend a more effective option than firePHP for showcasing server-side logs in the developer console on various browsers such as Firefox and Chrome for AJAX debugging? When it comes to using firePHP in Laravel, what is the preferred method? Sho ...

Adding custom fonts to DOMPDF: a step-by-step guide

First, I moved the dompdf library folder to /dompdf Next, I added a /fonts folder containing Roboto-Black.ttf Then, I created an index.php file <?php // Autoloader inclusion require_once 'dompdf/autoload.inc.php'; // Namespace reference ...

Using functions like nl2br() for new line breaks, preg_replace for replacing patterns, htmlspecialchars() for encoding special

I've reviewed all the answers on this topic, but I'm still struggling to find the correct method for sanitizing data. My task is to enter: <?php echo 'yes'; ?> <?php echo 'yes' ?> into a text area, submit it in ...

Is it possible to display images in PHP when the value matches the specified string?

Is there a faster and more efficient way to display one or multiple images if $value == $string? For instance, let's say I have a cell containing the string 'r o g'. If a user inputs ro, it should output <img src="red.gif"> and <im ...

Removing HTTPS from iframe in Zend 2

I'm having an issue with embedding an iframe in a Zend 2 view. No matter what I try, the frame is always displayed using http instead of https. I attempted to use the path in this way: echo '<iframe src="/path/to/iframe.php">'; and ...

Unable to establish a connection with standalone mongodb: No suitable servers were located

My debian development server is equipped with the standard MongoDB as well as PHP 5.4 and PECL MongoDB drivers v1.4.3. Attempting to establish a simple connection using PHP CLI resulted in an exception: php -r "MongoLog::setLevel(MongoLog::ALL); MongoLog: ...