I have this piece of code:
private function _resolveCustomEntries($fields)
{
foreach ($fields as &$value) {
if (array_key_exists('custom', $value)) {
if (method_exists($this, $value['custom'])) {
$value['custom'] = $this->$value['custom'](); //variableInterpolation
}
}
}
return $fields;
}
I ran a PHP 7.2 compatibility check and it complained here with the "variableInterpolation" on the marked line. When I run this code, the PHP log tells me this:
ERR (3): Notice: Array to string conversion in
/public_html/lib/KiTT/Payment/Widget.php on line 217
Thats the same line where the "variableInterpolation" check failed. So how would I rewrite this code so it works in PHP 7.2?
Thanks!
Solution:
$value['custom'] = $this->$value['custom']();
has to look like this:
$value['custom'] = $this->{$value['custom']}();
It's a matter of order variables are evaled.
With
class x {
public function y() {
echo 'ok';
}
}
$x = new x();
$y = array('i' => 'y');
Then
$x->$y['i']();
Fails because PHP first tries to cast the $y variable into a string, and get the matching property of $x (which btw does not exist), then tries to get the index 'i' or that unexisting property, and then tries to run it as a callable.
Hence 3 errors:
Array to string conversion
Undefined property x::$Array
Function name must be a string (nda: the undefined property returns NULL)
Instead, curly brace the variable to set the resolving order:
$x->$y['i']();
Will work. So use $this->{$value['custom']}()
This will throw an array to string conversion in 7.2
class bob{
function foo(){
return 'bar';
}
function getFoo(){
$value['custom'] = 'foo';
$value['custom'] = $this->$value['custom']();
return $value['custom'];
}
}
$bob = new Bob();
var_dump($bob->getFoo());
But it will execute just fine in 5.6.
Then i changed the snippet to this, not calling the method directly casting the array key to function name, but initializing a string (hopefully, there is no type validation in your code) variable with the function name first:
class bob{
function foo(){
return 'bar';
}
function getFoo(){
$value['custom'] = 'foo';
$functionName = $value['custom'];
$value['custom'] = $this->$functionName();
return $value['custom'];
}
}
$bob = new Bob();
var_dump($bob->getFoo());
This will run just fine in php 7.2
You could try rewriting your code using complex (curly) syntax, you can read more about it here.
Your code would look something like this.
$value['custom'] = $this->{$value['custom']}();
Edit: moved the curly braces to correct positions.
Related
I recently made a class in PHP
I am trying to declare a variable within class and using str_replace in a function but its show undefined variable
class Status{
$words = array(".com",".net",".co.uk",".tk","co.cc");
$replace = " ";
function getRoomName($roomlink)
{
echo str_replace($words,$replace,$roomlink);
}
}
$status = new Status;
echo $status->getRoomName("http://darsekarbala.com/azadari/");
Any kind of help would be appreciated thanks you
Your variables in the function getRoomname() aren't adressed properly. Your syntax assumes the variables are either declared within the function or passed while calling the function (which they aren't).
To do this within a class, do it while using $this->, like this:
function getRoomName($roomlink)
{
echo str_replace($this->words,$this->replace,$roomlink);
}
For further informations, please have a look into this page of the manual.
Maybe because of the version or something, when I tested your exact code, I got syntax error, unexpected '$words' (T_VARIABLE), expecting function (T_FUNCTION), so setting your variables to private or public should fix this one.
About the undefined varible, you have to use $this-> to access them from your class. Take a look:
class Status{
private $words = array(".com",".net",".co.uk",".tk","co.cc"); // changed
private $replace = " "; // changed
function getRoomName($roomlink){
echo str_replace($this->words, $this->replace, $roomlink); // changed
}
}
$status = new Status;
echo $status->getRoomName("http://darsekarbala.com/azadari/");
Also, since getRoomName isn't returning anything, echoing it doesn't do much. You could just:$status->getRoomName("http://darsekarbala.com/azadari/");.
or change to :
return str_replace($this->words, $this->replace, $roomlink);
I am having a problem with PHP that's confusing to me
Namely: Notice: Undefined variable: _GET in /var/www/dd.lo/app/libraries/system/input.php on line 86
pops up when you call:
$this->input->get('test');
The function calls another function (If my approach is bad please do not be mad. I will be happy if you tell me how to do it correctly):
public function get ($index)
{
return $this->_getArray('_GET', $index);
}
here is the code of the private function:
private function _getArray ($array, $index)
{
if (isset(${$array}[$index]))
{
return ${$array}[$index];
}
else
{
return NULL;
}
}
The Input class provides convenient access to _POST, _GET, _COOKIE and _SERVER data and allows you to avoid type checking:
if (isset($_POST['name']))
{
$name = $_POST['name'];
}
else
{
$name = NULL;
}
Incidentally, it requests a page at http://dd.lo/?test=dgdsgsdgsdgsd (i.e. $_GET, I asked)
If you write var_dump($_GET); then there is the index 'test'.
Apologies for the English, but I don't speak or read your language (Russian?). This answer is based on Google translate's version of what you asked.
PHP's super globals ($_GET, $_POST, etc.) are special variables, and it looks like you can't use these variables with PHP's variable variable feature. For example, this works
$foo = ['Hello'];
$var_name = 'foo';
var_dump($$var_name);
The "variable variable" $$var_name expands as $'foo'/$foo, and the variable dumps correctly.
However, the following does not work
$var_name = '_GET';
var_dump($$var_name);
It appears that whatever magic scope variable variables live in, that scope doesn't include the super globals. You'll need to rethink your approach. One way you might do this is by accepting the actual array instead of a string that's it's name, and specifying a "by reference" parameter in your function to avoid any performance issues
function _getArray(&$array, $key)
{
if(!is_array($array)) { throw new Exception("Invalid argumnet!");}
if(array_key_exists($key, $array))
{
return $array[$key];
}
return NULL;
}
I am getting an error in my actual code inside __isset, but when I went to a 'run php online website' it works. So I am not sure why it does not work in my code.
<?
class Test {
private $args;
public function __construct($args = array()) {
$this->args = $args;
}
public function __isset($name) {
return $this->args[$name]; //undefined index on my server (is fine on php site)
}
function prnt() {
// echo isset($this->i) ? "set" : "notset"; --> works, both print 'set'
echo isset($this->h) ? "set" : "notset";
}
}
?>
I then execute this:
$test = new Test(array('i' => '1234'));
$test->prnt();
//result on php website: notset
//result on my website: Undefined index at the line shown above.
Possibly helpful information:
My server is running php 5.1.
The isset($this->var) is happening in an include file in my actual code.
As long as the variable exists (like i above) it obviously works.
Your error reporting settings in each environment are different. One environment is allowing errors of E_NOTICE level through while another is blocking them.
You should do something like this:
return array_key_exists($name, $this->args);
You're trying to return the value of a non existent key, instead return the result of testing for the key using array_key_exists
public function __isset($name) {
return array_key_exists($name, $this->args);
}
I am getting the following error and I can't seem to figure out why or how it's getting triggered.
Fatal error: Cannot access empty property in /home/content/p/l/a/plai1870/html/com/php/Bone/Compiler.php on line 18
Line 18 is
throw new LogicException($this->$compilers[$language]." is not a supported compiler.");
Here is Compiler.php
<?php
namespace Bone;
use LogicException;
class Compiler implements \Bone\Interfaces\Compiler {
protected $compiler;
protected $compilers = array(
"php" => "PHP",
"as3" => "ActionScript3",
"javascript" => "Javascript"
);
public function __construct($language) {
$language = strtolower($language);
if (!isset($this->$compilers[$language])) {
throw new LogicException($this->$compilers[$language]." is not a supported compiler.");
}
$compiler = "\Bone\Compilers\\".$this->$compilers[$language]."\Compiler";
$this->compiler = new $compiler();
}
public function buildDefinition($object, $path = null) {
return $this->compiler()->buildInterface($object, $path);
}
public function buildObject($object, $path = null) {
return $this->compiler->buildObject($object, $path);
}
public function parameters($method) {
return;
}
public function save($data, $path) {
return;
}
}
?>
EDIT
And I'm calling it with:
$compiler = new \Bone\Compiler("php");
Sorry if this is the most obvious, but:
throw new LogicException($this->$compilers[$language]." is not a supported compiler.");
As it has been checked that the property does not exists, shouldn't it be:
throw new LogicException("$language is not a supported compiler.");
?
Edit:
$this->$compilers[$language]
^- variable property
Remove the $ there:
$this->compilers[$language]
Then you can check if the entry in the array is set, not if a propery with the name of the value inside the (unset) array $compilers (local variable) is set.
When developing, always switch on warnings and notices (highest error level you can imagine) to not run into these problems without having PHP warn you first.
Your array is $this->compilers, not $this->$compilers.
$compilers doesn't exist in your function, so $this->$compilers was looking for a blank property.
I have a simple question (i guess).
I'm getting the name of the function inside a variable from database. After that, i want to run the specific function. How can i echo the function's name inside the php file?
The code is something like this:
$variable= get_specific_option;
//execute function
$variable_somesuffix();
The "somesuffix" will be a simple text. I tried all the things i had in mind but nothing worked.
You want call_user_func
function hello($name="world")
{
echo "hello $name";
}
$func = "hello";
//execute function
call_user_func($func);
> hello world
call_user_func($func, "byron");
> hello byron
You want variable variables.
Here's some sample code to show you how it works, and the errors produced:
function get_specific_option() {
return 'fakeFunctionName';
}
$variable = get_specific_option();
$variable();
// Fatal error: Call to undefined function fakeFunctionName()
$test = $variable . '_somesuffix';
$test();
// Fatal error: Call to undefined function fakeFunctionName_somesuffix()
There are two ways you can do this. Assuming:
function foo_1() {
echo "foo 1\n";
}
You can use call_user_func():
$var = 'foo';
call_user_func($foo . '_1');
or do this:
$var = 'foo';
$func = $var . '_1';
$func();
Unfortunately you can't do the last one directly (ie ($var . '_1')(); is a syntax error).
You can also do sprintf($string).