I'm using an API that returns different values, and I want to dynamically tell my class to run a function with the same name (so i won't need a huge switch or if/else mess).
- How can I declare the object method
song.pause? - How can I use the object method
song.pause? - This might be an XY Problem, so is there a better way to do this? One alternative i thought of is to always call
str_replace('.','_',$type)and set functions without the periods. Thoughts?
Example:
<?php
class MyClass {
...
...
$type = "song.pause"; // This value is returned from another API I can't control
if (property_exists('MyClass', $type)) {
$success = $this->{$type}(); // ???
} else {
header("HTTP/1.1 400 Invalid type: $type);
exit;
}
public function 'song.pause'() { // obviously incorrect :)
???
}