PHP error: method name must be a string - php

I have found a bug report(http://bugs.php.net/bug.php?id=24286) about this though I am not sure if it's relevant to my problem and it's very old. This is my code:
class RegExp {
function name($e){ .... }
function email($e){ .... }
function phone($e){ .... }
}
$regexp = new RegExp();
$final_keys= array("name", "email", "phone");
for($index=0; $index < count($final_keys); $index ++){
if(!$regexp->$final_keys[$index]($_POST[$final_keys[$index]])){
$invalid_uri_vars .= $final_keys[$index].",";
}
else{
$uri_vars = "&".$final_keys[$index]."=".$_POST[$final_keys[$index]];
}
}
What it does is it uses a value from an array as the name of the method to be called. In other words, I am trying to implement a function call using a variable $final_keys[$index] as its name.
*UPDATE: I tried implementing the suggestions below, nothing seems to work. Here's one of my modifications as suggested:
for($key=0; $key < count($final_keys); $key ++){
if(!$regexp->{$final_keys[$key]}($_POST[$final_keys[$key]])){
$invalid_uri_vars .= $final_keys[$key].",";
}
else{
$uri_vars = "&".$final_keys[$key]."=".$_POST[$final_keys[$key]];
}
}
I get the same error as my original code. Another method using call_user_func but I am not sure if did it right:
for($key=0; $key < count($final_keys); $key++){
if(!call_user_func(array($regexp, $final_keys[$key]), $_POST[$final_keys[$key]])){
$invalid_uri_vars .= $final_keys[$key].",";
}
else{
$uri_vars = "&".$final_keys[$key]."=".$_POST[$final_keys[$key]];
}
}
And I get this error: Warning: call_user_func(Array) [function.call-user-func]: First argument is expected to be a valid callback in /.........testreqmeta.php on line 91

I'm still not getting any errors (other than the undefined $key in the second sample) when trying the sample code, so I can't say for certain what will fix it, but here's an approach that simplifies things and gets rid of the array indexing operation: use a foreach loop rather than a for loop.
$query = array();
foreach ($final_keys as $key) {
if(!$regexp->$key($_POST[$key])) {
$invalid_uri_vars[] = $key;
} else {
$query[] = "$key={$_POST[$key]}";
}
}
$uri_vars = implode('&', $query);
I've also replaced the repeated string appends with an array implosion, which should be slightly more performant. It also makes it easier to further process the query string, if necessary, before submitting it. There are better approaches yet, but that's a whole other topic.
NB. RegExp isn't a very good description of what the class does, hence it isn't a very good name. The class may use regular expressions, but it isn't itself a regular expression (which support operations such as "match" and "replace"; RegExp's API has none of these).

Quoting from your link:
ok, here is some workaround on this problem:
in case of
<?
class Test {
var $var = Array('test_function');
function test_function($echo_var) {
echo $echo_var;
}
}
$test_obj = new test;
$test_obj->var[0]('bla');
?>
you can avoid Fatal error in last string using this instead:
<?
$test_obj->{$test_obj->var[0]}('bla');
?>
So then:
if($regexp->{$final_keys[$index]}($_POST[$final_keys[$index]])){

There's a function for what you are trying to achieve call_user_func:
http://de2.php.net/manual/en/function.call-user-func.php

Related

How to avoid a really long list of function parameters in PHP?

My problem is that I have lots of functions with VERY long lists of function parameters such as this one:
function select_items($con,$type,$id_item,$item_timestamp,$item_source_url,$item_type,$item_status,$item_blogged_status,$item_viewcount,$item_language,$item_difficulty,$item_sharecount,$item_pincount,$item_commentcount,$item_mainpage,$item_image_width,$item_image_height,$item_image_color,$item_modtime,$order,$start,$limit,$keyword,$language,$id_author,$id_sub_category,$id_category,$id_tag,$id_user){ ... }
As you can see its super long and (of course) very hard to maintain. Sometimes I need all of the variables to construct a super complex sql query, but sometimes I just use 1 or 2 of them. Is there a way to avoid this colossal list of parameters? For example with some strict / special naming convention ?
So basically I need something like this:
$strictly_the_same_param_name="It's working!";
echo hello($strictly_the_same_param_name);
function hello() //<- no, or flexible list of variables
{
return $strictly_the_same_param_name; // but still able to recognize the incoming value
}
// outputs: It's working!
I thought about using $_GLOBALs / global or $_SESSIONs to solve this problem but it doesn't seems really professional to me. Or is it?
For a first step, as you said, sometimes you need to call the function with only 2 args, you can set default values to your arguments in the declaration of your function. This will allow you to call your function with only 2 args out of 25.
For example:
function foo($mandatory_arg1, $optional_arg = null, $opt_arg2 = "blog_post") {
// do something
}
In a second step, you can use, and especially for that case, arrays, it will be way more simple:
function foo(Array $params) {
// then here test your keys / values
}
In a third step, you can also use Variable-length argument lists (search in the page "..."):
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}
But ultimately, I think you should use objects to handle such things ;)
You can try use ... token:
$strictly_the_same_param_name= ["It's working!"];
echo hello($strictly_the_same_param_name);
function hello(...$args) //<- no, or flexible list of variables
{
if ( is_array( $args ) {
$key = array_search( 'What you need', $args );
if ( $key !== false ) {
return $args[$key];
}
}
return 'Default value or something else';
}

PHP - Simplify Way of Handling Arrays

When dealing with arrays I am forced to add a bunch of repetitive code to handle arrays with one child versus multiple:
//If more than one step, process each step, elcse processs single
if(!array_key_exists('command',$pullcase['steps']['step'])) {
foreach($pullcase['steps']['step'] as $step) {
$command=$step['command'];
$parameter=$step['parameter'];
if(isset($step['value'])){
$value = $step['value'];
$this->runCommands($command,$parameter,$value);
} else {
$this->runCommands($command,$parameter);
}
}
} else {
$command = $pullcase['steps']['step']['command'];
$parameter = $pullcase['steps']['step']['parameter'];
if(isset($pullcase['steps']['step']['value'])){
$value = $pullcase['steps']['step']['value'];
$this->runCommands($command,$parameter,$value);
}
else { $this->runCommands($command,$parameter); }
}
As you can see, I'm having to duplicate my efforts depending on if there is a single item in an array versus multiple:
$pullcase['steps']['step'][0]['command']
vs
$pullcase['steps']['step']['command']
How can I simplify this code so that I can use a single variable for all instances?
If you control the creation of the array, make step an array of one even if there is only one so you always have an array. Is that possible?
You either have a step array [step][0][command] or you have a single step [step][command]. So when you create the array instead of [step][command] make it [step][0][command] etc. Standard way of doing it, problem solved as you only need the foreach.
If you can't do it at array creation then consider doing it before the loop:
if(is_array($pullcase['steps']['step'])) {
$steps = $pullcase['steps']['step'];
} else {
$steps[] = $pullcase['steps']['step'];
}
foreach($steps as $step) {
$value = isset($step['value']) ? $step['value'] : null;
$this->runCommands($step['command'], $step['parameter'], $value);
}
Also, if runCommands() can detect a empty argument, then an alternative to the if/else for the function call is used above.
The following may help. It doesn't do much but call the function "runcommands" on a value if the key is 'command'. I am using it to show how you can use array_walk_recursive to possibly solve your problem.
First, you need the function to use:
function runcommandswhencommand($value, $key)
{
if($key == 'command') runcommands($value);
}
Now, you can use the recursive walk on your array:
array_walk_recursive($pullcase, 'runcommandswhencommand');
With this, whenever the key is 'command', the value of that index will be used in the parameter of the function runcommands().

PHP 5.6 Generator Syntax: Can generators only be used in foreach loops?

I know that this can be super easily archieved without generators, however I want to understand generators better. Therefore please don't suggest using something else.
I've got a class that generates filenames for screenshots (selenium):
class ScreenshotName
{
private $counter = 0;
public function screenshotNameIterator()
{
while(true) {
yield sprintf("screenshot-%s-%s.png", date("Y-m-d\\TH:i:s"), ++$this->counter);
}
}
}
Now my question is: can I use such a generator in any other context than a foreach loop? e.g.
(new ScreenshotName())->screenshotNameIterator()->next()
for me this always returns null, and if I debug, it never enters the generator method. Also the PHP docs don't really mention this.
So my question is: is there a documented way to use a generator in a different context than a for-loop?
There is a documented way to do this. In fact Generator does implement the iterator interface as you can see it on this page.
In fact the foreach keyword only work on iterators. So if you can use foreach on a generator you must be able to call next
Here is a sample code using next instead of foreach :
<?php
function evenNumbers() {
for ($i = 0;; $i++) {
yield 2*$i;
}
}
$gen = evenNumbers();
$gen->next();
echo $gen->current();
?>
Here is a complete code snippet to go with Dark Duck's answer. That is, it demonstrates how to use Iterator interface to iterate over all values. (Dark Duck's code is not complete.) Based on a comment by robert_e_lee on Iterator doc:
$gen = evenNumbers();
// If in a method that re-uses an iterator, may want the next line.
//OPTIONAL $gen->rewind();
while ($gen->valid()) {
// This is the value:
$value = $it->current();
// You might or might not care about the key:
//OPTIONAL $key = $it->key();
// ... use $value and/or $key ...
$it->next();
}
The above is roughly equivalent to:
foreach (evenNumbers() as $value) {
// ... use $value ...
}
or
foreach (evenNumbers() as $key => $value) {
// ... use $value and/or $key ...
}

make conditions with logical operators into variable

i want to make condition with logical operators (&& or ||) into one varialbe with concatination, and then run into if ($var)...
<?php
$ar = array(
'index1' => array('bedrooms'=> '1','suburb' => 'one'),
'index2' => array('bedrooms'=>'2', 'suburb'=>'two')
);
$userValBed = '1';
$userSuburb = 'one';
$c = '';
$c .= '($value["'.'bedrooms'.'"] == "'.$userValBed.'") && ';
$c .= '($value["'.'suburb'.'"] == "'.$userSuburb.'")';
foreach($ar as $key => $value){
if($c): // here occurs problem, please fix this.
// if($value["bedrooms"] == "1" && $value["suburb"] == "one"): // comment this line, and uncommet above 'if'.
echo "<br>";
echo "this condtions matches to ";
echo $key . ' key '. '<br>';
endif;
}
?>
What you could use is a Command pattern. The command pattern seeks to wrap up functionality inside of a class. The advantage of this is that you can swap out functionality at run time by specifying what you would like in the config. Here is a more in depth explanation.
class Command {
function doSomething($v1, $v2);
}
Then you create sub classes of that class:
class CommandAdd extends Command
{
function doSomething($v1, $v2)
{
return $v1 === $v2;
}
}
You can make as many classes with doSomething as you want. Then all you have to do is define your functionality:
$command = new CommandAdd();
if($command->doSomething()) {
// Code here
}
You can specify the type of Command in an external file, or something similar.
Putting code into variables is not a good design. It may work, but it's ugly and hard to understand.
Instead, you may use inner foreach for checking wheter the user's parameters matches the item's parameters.
Anyway, it's even better to store the data in some kind of database, say, SQL database, so the problem will change to: how to create SQL query from user's parameters.

dynamically retrieve object method

I try to dynamically retrieve a method of a class but php throws an exception which says Undefined property: stdClass ...
and How i try to get the values
private function getExactValue($row, $name)
{
$tempRow = clone $row;
foreach( explode('->', $name) as $key => $value)
{
$temp = $tempRow->{$value};
unset($tempRow);
$tempRow = $temp;
}
return $tempRow;
}
$row is an instance of an Object (not Std one)
$name is what i need in the Object to traverse , for example when i need $row->student->gifts->totalPoint() just pass the student->gifts->totalPoint() to the method for $name parameter
can you tell me what my mistake is?
I see what you are trying to do here. My first word of advice is that you are going about what you are trying to achieve in a very hackish way. If you wanted a better way to be able to execute arbitrary methods on an unknown object, I would suggest you look into PHP's reflection capabilities.
That being said, the problem with your code would appear to be that you are trying to execute a method via string, where what you need to do is utilize the method's name. What I would suggest is that within your loop where you explode the string on ->, you try to detect if it is a method or not, and then act accordingly. That could look like this:
foreach( explode('->', $name) as $value)
{
$value_trimmed = rtrim($value, '()');
if ($value === $value_trimmed) {
// this is a property
$tempRow = $tempRow->{$value};
} else {
// this is a method
$tempRow = $tempRow->{$value_trimmed}();
}
}
You should probably also do some validation on the input as well to make sure you have valid property/method names for each segment, as well as add validation that the entire string is indeed properly formed (i.e. you don't have things like foo->->bar(())). Of course this make no mention of how to handle array like foo[0]->bar() which you might also need to accommodate.

Categories