Global Class Methods?
I want to be able to call my php class via an http post. So I came up with this.
$method = $_POST['method'];
$data = $_POST['data'];
$class_methods = get_class_methods('myclass');
if($method, $class_methods){
$save = $myclass::$method($data);
echo $save;
}
Is there any global methods that exist on classes in general that might allow someone to call a 开发者_如何学运维method I have not defined?
Thanks all.
If I understand you correctly, have a look at:
http://php.net/manual/en/language.oop5.magic.php
Constructs like:
<?php
class Foo
{
public static function bar() {
}
}
$className = 'Foo';
$method = 'bar';
$return = call_user_func_array(array($className, $method), array() /* << the params go here */);
are also possible, and on PHP 5.3+ you can do:
$return = $className::$method();
directly.
I believe this should work for you
__call and __callStatic
There's a magic method __call()
which is called when you try to invoke a method that hasn't been defined.
However, you need to explicitly assign that __call()
method to your class otherwise you'll get an error that you're calling an undefined method.
精彩评论