PHP oop how can i call dynamic function name - php

I have a method display() in the book class.
$name = 'display()';
$book = new Book('PHP Object-Oriented Solutions', 300);
$book->$name;
How can i call display method using $book->$name

You need to tell PHP that you're trying to execute a method, not in the variable itself, but within the actual code:
$name = 'display';
$book = new Book('PHP Object-Oriented Solutions', 300);
$book->$name();
Otherwise, as you have seen, it will treat $name as a property name, and rightly so ... If you have both a property and a method named 'display', there wouldn't be a way to distinguish between the two using what you've tried.

The cleanest way (imo at least) is to use something like:
$name = 'display';
$book = new Book('PHP Object-Oriented Solutions', 300);
call_user_func([$book, $name]); // This looks cleaner and/or more obvious on first sight.
// call_user_func_array([$book, $name], $arrayOfArguments);
// And as #Narf suggested (and I agree cuz we move forward)
call_user_func([$book, $name], ... $arrayOfArguments);
And yes you can pass parameters to this function, that will be passed to the function, but you have to list them after the callable array. in order to avoid doing that (hard to maintain and not always what you want) you can use call_user_func_array which accepts an array of arguments as second argument that is passed to the callable.
call_user_func Documentation
call_user_func_array Documentation

Related

Call a static method, where the method name is stored as a variable

How can I call a class's static method, when the method I want to call is stored in a variable?
For example:
$action = "myMethod";
MyClass::$action("some argument");
Which could result in the same as doing this:
MyClass::myMethod("some argument");
You do that in this way:
call_user_func(array('MyClass', $action), 'some argument');
References:
http://php.net/call_user_func#example-5359
I am not quite sure if you're talking about Delegates or not. But Storing a function/method in a Variable is called Delegates. I will post 2 examples.
require_once('classes.php');
$pkg = new Package("Heavy Package");
$pkg->setWeight(100);
$shipper = new ShippingDelegate();
if ($pkg->getWeight() > 99)
{
$shipper->useRail();
}
$shipper->deliver($pkg);
The first one has a parameter for the Method class. The Other One calls its class Constructor.
I hope this helps.

Read the array inside the query scope

I have an array in $genreArr, i want it to be readable in the line whithin the function(query) inner select:
if($genreArr){
$movies = DB::table('movie_list')->whereIn('movie_id',function($query){
$query->select(DB::raw('movie_id'))
->from('movie_genre')
->whereIn('genre_id',$genreArr)
->distinct();
})->orderBy(DB::raw('RAND()'))->take(10)->get();
}
How do i do that without using global variables, i tried pass that variable as the second param next to the $query and than it gives me this kind of error:
Missing argument 2 for MovieController::{closure}()
You should adjust the closure to use the variable you want.
function ($query) use ($genreArr) {
... ->whereIn('genre_id', $genreArr);
}
Not sure whether the whereIn method will accept this. But rather than using a closure, you could use an alternative callable option. i.e Something like
$obj = new SomeClass();
$obj->setGenreArray($genreArr);
$movies = DB::table('movie_list')->whereIn('movie_id',array($obj, 'getQuery'))->orderBy(DB::raw('RAND()'))->take(10)->get();
And then you're query builder stuff will be inside the getQuery method, with access to the property $this->genreArray

Call methods by defined names & add extra parameter before being called

I have a Validator class that can build several arrays with methods (names) stored.
Like so $this->rules[1] = ['trim()', 'required()', 'max(35)'];
How can I loop through every method the array and call it exactly by how they are defined?
If I do it like the following, I get Undefined property: Validator::$trim() etc. error.
foreach ($this->rules[1] as $method) {
$this->$method;
}
How can I add an extra parameter $input to each method in the array before it gets in the loop?
So instead of trim() it would be trim($input), and for max(35) max(35, $input), etc.
First of all, use $this->{$method} to call your method in your example.
Secondly, don't call them like that at all. Use call_user_func_array instead.
You need to extract method name and parameters frist in order to call directly.
I recommend you use a placeholder for your $input to add it to your method call.
You can then pass the parameters for your function call as an array.
$ruleset = 'max(34,:input)';
// do the string manipulation yourself please ;-)
$method = 'max';
$input = getInput(); // wherever you get that from
$parameters = array (34, $input),
call_user_func_array(__NAMESPACE__ .'\Validator::' . $method, $parameters);
What you are looking for are the call_user_func and/or call_user_func_array methods.
As Andresch pointed out, the way your rules are defined aren't flexible. Now you would have to parse them to retrieve the inputs for the function. a better way would be the following format:
$this->rules[1] = array(
'trim',
'required',
array('max'=>35)
);
and then
foreach ( $this->rules as $rule )
{
if ( is_array($rule)
{
call_user_func_array(array($this, key($rule)), current($rule));
}
else
{
call_user_func(array($this,$rule));
}
}
P.S. don't just copy paste this code, this is intended for explanation

PHP: Passing functions to a class

I made a class. I give it some objects (mostly retreived database rows) as input, and tell it which fields it has to show, and which buttons I want. Then it renders a very nice html table! It's pretty awesome, I think.
$ot = new ObjectTable();
$ot->objects = $some_objects;
$ot->fields = array('id','name','description','image');
$ot->buttons = array('edit','delete');
$ot->render();
However, I also want to be able to manipulate the data it shows. For example, i want to be able to truncate the 'description' field. Or to display an image thumbnail (instead of 'picture.jpg' as text). I don't know how to pass these functions to the class. Perhaps something like:
$ot->functions = array(null,null,'truncate','thumbnail');
But then I don't know how to convert these strings to run some actual code, or how to pass parameters.
There must be a nice way to do what I want. How?
Check this question and the answer is:
As of PHP5.3 you could use closures
or functors to pass methods
around. Prior to that, you could write
an anonymous function with
create_function(), but that is
rather awkward.
But what you are trying to achieve is best solved by passing Filter Objects to your renderer though. All filters should use the same method, so you can use it in a Strategy Pattern way, e.g. write an interface first:
interface Filter
{
public function filter($value);
}
Then write your filters implementing the interface
class TruncateFilter implements Filter
{
protected $_maxLength;
public function __construct($maxLength = 50)
{
$this->_maxLength = (int) $maxLength;
}
public function filter($value)
{
return substr(0, $this->_maxLength, $value) . '…';
}
}
Give your ObjectTable a method to accept filters
public function addFilter($field, Filter $filter)
{
if(in_array($field, $this->fields)) {
$this->_filters[$field][] = $filter;
}
return $this;
}
And when you do your ObjectTable instantiation, use
$ot = new ObjectTable();
$ot->objects = $some_objects;
$ot->fields = array('id','name','description','image');
$ot->addFilter('description', new TruncateFilter)
->addFilter('name', new TruncateFilter(10))
->addFilter('image', new ThumbnailFilter);
Then modify your render() method to check if there is any Filters set for the fields you are rendering and call the filter() method on them.
public function render()
{
foreach($this->fields as $field) {
$fieldValue = // get field value somehow
if(isset($this->filters[$field])) {
foreach($this->filters[$field] as $filter) {
$fieldValue = $filter->filter($fieldValue)
}
}
// render filtered value
}
}
This way you can add infinite filters.
PHP has a pseudo-type called "callback", which is actually an ugly closure in disguise. You can call such callbacks using call_user_func() or call_user_func_array():
$callback = 'strlen';
echo call_user_func($callback, '123');
// will output 3 (unless you're using a strange encoding)
You are looking for create_function().
However, creating functions on runtime and adding them to a class doesn't sound right to me. It's likely to become a maintenance nightmare very quickly. There are better ways to achieve what you want. What kind of manipulation would the functions to to the data? How about storing them in a "tools" class and connecting that with the table object when needed?
$functions = array(null,null,'truncate','thumbnail');
$function_1 = $functions[3];
$my_string = 'string to truncate';
$result = call_user_func($functions[2], $my_string);
If you want to pass multiple parameters, use call_user_func_array instead.
You might also want to explore call_user_func, which allows you to call a function based on a string representing its name.

Passing an Array as Arguments, not an Array, in PHP

I seem to remember that in PHP there is a way to pass an array as a list of arguments for a function, dereferencing the array into the standard func($arg1, $arg2) manner. But now I'm lost on how to do it. I recall the manner of passing by reference, how to "glob" incoming parameters ... but not how to de-list the array into a list of arguments.
It may be as simple as func(&$myArgs), but I'm pretty sure that isn't it. But, sadly, the php.net manual hasn't divulged anything so far. Not that I've had to use this particular feature for the last year or so.
http://www.php.net/manual/en/function.call-user-func-array.php
call_user_func_array('func',$myArgs);
As has been mentioned, as of PHP 5.6+ you can (should!) use the ... token (aka "splat operator", part of the variadic functions functionality) to easily call a function with an array of arguments:
<?php
function variadic($arg1, $arg2)
{
// Do stuff
echo $arg1.' '.$arg2;
}
$array = ['Hello', 'World'];
// 'Splat' the $array in the function call
variadic(...$array);
// 'Hello World'
Note: array items are mapped to arguments by their position in the array, not their keys.
As per CarlosCarucce's comment, this form of argument unpacking is the fastest method by far in all cases. In some comparisons, it's over 5x faster than call_user_func_array.
Aside
Because I think this is really useful (though not directly related to the question): you can type-hint the splat operator parameter in your function definition to make sure all of the passed values match a specific type.
(Just remember that doing this it MUST be the last parameter you define and that it bundles all parameters passed to the function into the array.)
This is great for making sure an array contains items of a specific type:
<?php
// Define the function...
function variadic($var, SomeClass ...$items)
{
// $items will be an array of objects of type `SomeClass`
}
// Then you can call...
variadic('Hello', new SomeClass, new SomeClass);
// or even splat both ways
$items = [
new SomeClass,
new SomeClass,
];
variadic('Hello', ...$items);
Also note that if you want to apply an instance method to an array, you need to pass the function as:
call_user_func_array(array($instance, "MethodName"), $myArgs);
For sake of completeness, as of PHP 5.1 this works, too:
<?php
function title($title, $name) {
return sprintf("%s. %s\r\n", $title, $name);
}
$function = new ReflectionFunction('title');
$myArray = array('Dr', 'Phil');
echo $function->invokeArgs($myArray); // prints "Dr. Phil"
?>
See: http://php.net/reflectionfunction.invokeargs
For methods you use ReflectionMethod::invokeArgs instead and pass the object as first parameter.

Categories