0

I am pretty new to PHP and I am looking at a way to recurse an array and go to a specific function depending on the contents of the array.

For example if I have an array with the 3 values of 'email, name, password '

I would need my code to run each of them functions.

The following code is what I currently have.

<?php
$a = $_POST;
/**
*datavalidation
*/
class validation
{
    //  private $output = [];
    public $data = [];

    function __construct($data)
    {
        $this->data= $data;
        $this->begin();
    }

    function begin()
    {

        foreach ($this->data as $key => $value) 
        {
            $this->$key;
        }
        $this->__destruct();
    }

}
4
  • 4
    What functions are you talking about? How do they relate to the elements of that array? Commented Sep 20, 2018 at 9:38
  • Just call the function in the foreach, as you do in the __construct like $this->begin();. Is this what you want or somethink special? Commented Sep 20, 2018 at 9:40
  • the function name isn't important, The function will be called the same as the key of the array Commented Sep 20, 2018 at 9:41
  • it's better to create a generic function and pass a field parameter, rather than try to call dynamically named functions .. Commented Sep 20, 2018 at 9:42

3 Answers 3

1

To call a function dynamically, you also need to execute the function, else you call a property, so:

foreach ($this->data as $key => $value) 
{
    $this->$key;
}

Should be

foreach ($this->data as $key => $value) 
{
    $this->$key();
}
Sign up to request clarification or add additional context in comments.

4 Comments

In the instance where the $key is = the 'name' will this call up the function name(); ? - thanks.
Yes, that is exactly what is does, for more info: php.net/manual/en/functions.variable-functions.php#example-160
One last question, In the instance where Name isn't a function but is a key would this cause an error? and if so how would i ignore this? Would I just use a try catch around this loop?
You could use php.net/manual/en/function.method-exists.php to check if the method exists in the object
1
    foreach ($this->data as $func_name => $values){
        call_user_func($func_name, $values);
    }

Comments

0
function begin() {
    foreach ($this->data as $key => $value) {
       $this->$key();
    }
}

Beside this, why are you using the constructor to execute the operation? First create the object and, after the initialitation, call the methods of the object until it is not needed anymore. It will be easier to handle the exceptions and debug the application.

$obj= new Validation();
$obj->begin();
or in a single line:
(new Validation())->begin();  

Note: the second method is available since 5.4 (reference)

Furthermore, it is not necessary to call to the destructor from the ownner object (now you are calling the desctructor in the constructor!! ctor() -> $this->begin() -> $this->__destruct() . Someone using your object outside will call the desctructor (on an implicit or explicit way).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.