1

When I use the code below it throws an error even though the function looks well formed. (FYI I also tried valid and static prefixes.)


Function call:

foreach ($this->errorList as $field => $error_msg) {
            // check for valid input and store cleaned vars
            $this->goodInput[$field] = Valid::__($field);
}

Result:

PHP Fatal error: Call to undefined function Valid::email()


Code:

class Valid
{
    public static function __($name)
    {
        // this is what I tried:
        // $checkFunction = 'self::' . $name;
        // return $checkFunction();

        // this works:
        // Thank you to fusion3k and Darren
        return self::$name();
    }

    private static function email()
    {
        //sanitize and validate email field
    }

    // other methods for other fields
    // ...

    // default method to sanitize input for display in email
    public static function __callStatic($name, $arguments)
    {
        if (array_key_exists($name, $_POST)) {
            $clean_var = trim($_POST[$name]);
            $clean_var = stripslashes($clean_var);    
            $clean_var = htmlspecialchars($clean_var);
            return $clean_var;
        } else {
            return '';
        }
    }
}
6
  • 1
    I am not sure how you do a dynamic static method like that but it won't be like that. You can't substitute 'Valid::' for Valid:: Commented Mar 15, 2016 at 3:04
  • What would happen if you replaced Valid:: with self:: in your first function? Commented Mar 15, 2016 at 3:04
  • Simply use return self::$name(); (no $checkFunction needed) (see demo) Commented Mar 15, 2016 at 3:06
  • 1
    @Rasclatt There's lots of potential problems with his code, but I think you can call a function like that. php.net/manual/en/functions.variable-functions.php Commented Mar 15, 2016 at 3:06
  • 1
    I see in v7 it appears to be able to handle a stringed class variable like the OP is demonstrating in example #4. I think a better option is the self::$name option Commented Mar 15, 2016 at 3:13

2 Answers 2

3

I assume you instantiate your class somewhere properly first right? Well it's best to check that a said method exists, and harness self::. Also, as @fusion3k said, better to return self::$name(). Here's an example of what you should be doing:

public static function __($name) {
    if(method_exists(new self, $name)) {
        return self::$name();
    }
}

In all honesty, this isn't the best way for you to be doing it. You should be looking into call_func_user_array() to manage this kind of implementation properly. Allowing the parsing of arguments to called methods.

Sign up to request clarification or add additional context in comments.

4 Comments

I am looking into call_func_user_array() but I have no idea what I'm doing. (I am a dilettante programmer.) Would that function make sense in the context I am calling from? See my edit.
Also, why would I need to check if method exists if I am using the call static catch all?
@Pellinore read up on method_exists() - it essentially makes sure that your email() method, or any methods you're trying to call, exist inside the class, so that you don't run into any issues in the future.
I was actually intentionally using the callStatic method as the default handler. For strings for instance, like name, message, and subject. No reason to have separate methods for those.I suppose I could use an if-else tree to send it to the right method, but I felt this was cleaner.
0

There is not any object created when you call an static function then Valid functions are not accessible. Try like this

public static function __($name) { 
$valid = new Valid ();
return $valid::$name(); 
}

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.