Executing a PHP function that invokes itself with an identical set of arguments

I have a PHP function with the ability to handle a dynamic number of arguments by using the func_get_args() method.

class Test {

    private $flag = FALSE;

    public function test() {
        $arguments = func_get_args();
        if ($this->flag) {
            var_dump($arguments);
        } else {
            $this->flag = TRUE;
            $this->test($arguments); //I want to recursively call the function with the same arguments. (this is pseudo-code)
        }
    }

}

It's important to note that this function is not truly recursive due to the "$flag" variable preventing it from iterating multiple times.

My objective is to have the test() function invoke itself using the exact set of arguments initially provided. For instance, when calling Test->test("a", "b", "c");, the expected output would be:

array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" }

Answer №1

If you are seeking a straightforward response to the query highlighted in the heading, here is an approach that invokes the current class method using the same arguments passed to it:

To call the class method: call_user_func_array([ $this, __FUNCTION__ ], func_get_args());

In case of a simple function (not embedded within a class), this can be done:

To call the function: call_user_func_array(__FUNCTION__, func_get_args());

Answer №2

Utilize the call_user_func_array function.

For example:

class UniqueClass {

    private $indicator = FALSE;

    public function uniqueFunction() {
        $arguments = func_get_args();
        if ($this->indicator) {
            var_dump($arguments);
        } else {
            $this->indicator = TRUE;
            call_user_func_array(array($this, 'uniqueFunction'),$arguments);
        }
    }

}

$uniqueObject = new UniqueClass();

//Displays array(3) { [0]=> string(6) "bananas" [1]=> string(5) "apples" [2]=> string(8) "oranges" }
$uniqueObject->uniqueFunction("bananas","apples","oranges");

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

Shared class attribute on a PHP object

Does PHP allow for a class to store an object of the same class? Is there a method to achieve this behavior in PHP since it doesn't support pointers like C++? ...

Exploring the Amazon Product Advertising API for the best deals on your first purchase

I am utilizing the Amazon Product Advertising API PHP Class provided by Codediesel for my project. Specifically, I have implemented the function getItemByAsin and modified the ResponseGroup to Offers. This adjustment allows me to access the IsEligibleForP ...

A single-row result is returned by a MySQL query

I'm running into an issue with my query where I seem to be getting only the last row instead of all three available rows from the table. Can someone help me identify what mistake I might have made in my code? Here's the snippet: $db = new mysqli ...

Tips for properly passing data from Ajax to PHP and extracting value from it

I'm curious about how to receive a value from Ajax and then send that value to PHP. Can anyone provide some guidance on this? Specifically, I need the percent value obtained from Ajax to be sent to $percent for option value. <div class="form-g ...

ReactJS encountered an error: _this3.onDismissID is not defined as a function

My goal is to retrieve the latest news related to a specific search term from a website, showcase them, and provide a dismiss button next to each news item for users to easily remove them if desired. Here's a snippet of the code I'm using: import ...

Switch Your PHP Script to Use PDO

Hello, I am new to PHP and seeking some guidance. My goal is to convert the following code to PDO in order to generate a JSON output for an Android app that I am currently working on. I have tried several solutions but encountered issues with the JSON resp ...

Why is the var_dump returning null?

Greetings, I am currently utilizing Zend Framework (PHP) and encountering an issue with the output of a select option value in var_dump after sending a POST request. Here is the code snippet: <div class="entry"> <form action="<?php echo $this ...

Adding new data to Codeigniter database

Having recently started to work with codeigniter, I have encountered a challenge when it comes to inserting data into my MySQL database. While I am able to retrieve information from the database through a controller called home.php using a model I create ...

Saving and accessing numbers in MySQL database

I'm encountering an issue with integers in MySQL. I'm attempting to update a cell that stores an integer value, but the type of the cell seems to be causing problems. Despite being set as int, whenever I retrieve the data, it always shows up as 0 ...

Submitting a specific group of form inputs using ajax involves selecting the desired elements within the form

My task involves dealing with a large form that needs to be submitted in PHP, which poses a challenge due to the maximum input variable limits in PHP +5.3 as discussed on this Stack Overflow thread. Instead of submitting everything from the form, I only n ...

Error Alert - Troubleshooting AJAX JSON Parsing Issue

I've encountered a problem with AJAX that involves parsing a JSON Array from a webservice I am developing. The front-end of my project uses a simple combination of ajax and jQuery to showcase the results retrieved from the webservice. Despite being c ...

Utilizing PHP, Javascript, and jQuery in mobile technology gadgets

Can anyone recommend mobile technology products that have been developed using PHP, Javascript, and jQuery? What are the newest mobile products available on the market that have been built using these languages? Do popular devices like iPhone, BlackBerry ...

Getting the current row values in Yii2 Gridview

Recently, I started working with Yii2 and encountered an issue with a gridview I am using. In this gridview, two columns are auto-generated rather than being included in the model class. Each row contains a button that, when clicked, should allow me to acc ...

Error: Selenium unable to locate Firefox user profile

Recently, I embarked on my journey of learning Selenium on a Linux server. My first step was to open two Putty terminals and navigate to the directory where my files are located. In terminal 1, to start the server, I executed the following command: DISPL ...

PHP MYSQL, streamlined alert system

Could someone assist me in removing the notification counts after they have been read or opened? I apologize if the explanation is unclear and for any language mistakes. Here are a sample of my codes: /index.php <script src="http://ajax.googleapis. ...

Steps for assigning 'id' attribute to elements in XML by using values from other elements

Can someone assist me in achieving the desired outcome below? I am looking for a way to add the id attribute to the <image> tags using PHP or any other methods. <?xml version="1.0" encoding="utf-8"?> <root> <property> ...

Issue with Laravel Composer: Failed to install, restoring original content in ./composer.json

I am fairly new to Laravel and I have been using Laravel 5.8 for my project development. Recently, I needed to access the file ExampleComponent.vue in the resources/js/components directory but it was not there. After referencing this link, I came to know ...

Transmitting unique characters, such as a caron symbol, via xmlhttp.responseText and encoding them with json_encode

I am struggling to retrieve data from a database that contains a special character (caron) and then pass it through xmlhttp.responseText using json_encode to fill textboxes. However, the textbox linked to the data with the special character (caron) is not ...

Display the input text value when the button is clicked

I am a beginner in JavaScript and have created this HTML page: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> Upon entering text into the input field and clicking on the submi ...

Class Mapping Titles

I'm attempting to extract the values of attributes and elements from an XML response using a PHP SOAP client. I've been trying to utilize classmap, but haven't had any success so far... SOAPCLIENT: $client = new MySoapClient(null, array(&a ...