Context: On one of the sites where my WordPress plugin is installed I'm seeing a series of PHP warnings, but I'm not entirely sure why this is happening. I'm hoping someone here can help me figure out how to solve this warning.
Code Sample:
function my_function( $array ) {
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
The Warning:
Warning: Illegal string offset 'where' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 14
Warning: Illegal string offset 'echo' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 15
Warning: Cannot assign an empty string to a string offset in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 15
Warning: Illegal string offset 'content' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 16
Warning: Cannot assign an empty string to a string offset in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 16
Warning: Illegal string offset 'shortcode' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 18
Warning: Illegal string offset 'devs' in
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
on line 19
For some reason, it's throwing a warning every time it encounters one of the indices in the array. How do I fix this? Thanks!
It looks like the function is expecting an array and is getting a string instead.
You can require an array in the function definition.
function my_function(array $array) { ...
Then if you call it with something other than an array, you'll get a TypeError.
Unfortunately, there will still be a problem somewhere else in your code, where something you thought was an array is actually a string.
Setting up your function like this will generate an error earlier, which is good, because it will make it more obvious where the problem is. If you modify your function to ignore the problem instead, it will probably just create more confusing behavior and potentially different errors.
The usage of the function is_array() in the beginning of your function can give you the insurance, that, if someone pass you something else than an array, the variable is reinitialised as an empty array.
Unsetting or nulling it before doing that is useless, because, as of PHP 5.3, PHP does have a garbage collector mechanism.
/**
* #params array $array
*/
function my_function( $array ) {
if ( ! is_array ( $array ) ) { $array = [] };
/**
* Or, if you don't like the short array notation:
* if ( ! is_array ( $array ) ) { $array = array(); };
*/
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
Related
I am trying to add a value to the end of an array. I am using:
array_push($this->_attributes["class"],$value);
Now I know that the first parameter has to be an array. Upon inspection :
var_dump($this->_attributes["class"]); die(0);
array_push($this->_attributes["class"],$value);
I can see that the value being passed in is indeed an array as it should be. I am not sure how or why I am getting a string being passed. The output of the var_dump look like such:
array (size=0)
empty
Why or how is $this->_attributes["class"] being seen as a string and not an array?
Edit:
If I invert the two lines like so:
array_push($this->_attributes["class"],$value);
var_dump($this->_attributes["class"]); die(0);
The var_dump looks like this:
array (size=1)
0 => string 'btn' (length=3)
This is the expected output. If I remove the var_dump, I get a fatal error on the array_push again.
** Full Class Declaration**
This is enough of the class I able building for this example:
class Tag
{
protected $_attributes = array("class"=>array());
public function setAttribute($attribute,$value)
{
if( $attribute === "class" ) {
$this->setClassAttribute($value);
}
$this->_attributes[$attribute] = $value;
}
public function setClassAttribute($value)
{
if( is_array($value) ) {
foreach ($value as $c) {
$this->setClassAttribute($c);
}
return;
}
// var_dump($this->_attributes["class"]); die(0);
array_push($this->_attributes["class"],$value);
// var_dump($this->_attributes["class"]); die(0);
}
}
To execute it:
$tag = new Tag();
$tag->setAttribute("class","btn");
Your problem is here:
public function setAttribute($attribute,$value)
{
if( $attribute === "class" ) {
$this->setClassAttribute($value);
}
$this->_attributes[$attribute] = $value;
}
setClassAttribute is indeed setting the value to array("btn"). Afterwards, it's being overwritten by the line outside the statement. Try:
public function setAttribute($attribute,$value)
{
if( $attribute === "class" ) {
$this->setClassAttribute($value);
} else {
$this->_attributes[$attribute] = $value;
}
}
REPL:
php > $tag = new Tag();
php > $tag->setAttribute("class","btn");
array(0) {
}
array(1) {
[0]=>
string(3) "btn"
}
php > $tag->setAttribute("class","btn");
string(3) "btn"
Warning: array_push() expects parameter 1 to be array, string given in php shell code on line 21
string(3) "btn"
I am getting this message:
PHP Notice: Undefined offset: 1 in
/home/printet1/public_html/wp-content/mu-plugins/gd-system-plugin/class-gd-system-plugin-config.php on line 56
The relevant code is:
public function get_config( ) {
if ( empty( $this->config ) ) {
$defaults = $this->_get_config( '/web/conf/gd-wordpress.conf' );
$resellers = $this->_get_config( '/web/conf/gd-resellers.conf' );
$reseller = null;
if ( defined( 'GD_RESELLER' ) && is_numeric( GD_RESELLER ) ) {
$reseller = $resellers[GD_RESELLER];
}
if ( is_array( $reseller ) && !empty( $reseller ) ) {
$this->config = array_merge( $defaults, $reseller );
} else {
$this->config = $defaults;
}
}
return $this->config;
}
With line 56 specifically being:
$reseller = $resellers[GD_RESELLER];
I'm relatively new to coding and would like any information/help on what I can do to fix this. I have read explanations on what is going wrong but don't understand what to do in order to address this issue.
Thanks in advance for any help!
It seems that $resellers array doesn't hold the value you are looking for.
one way to debug this, is to add
var_dump(GD_RESELLER);
var_dump($resellers);
to line 55, just before this line:
$reseller = $resellers[GD_RESELLER];
to get an idea of what's going on.
if 'GD_RESELLER' is a number, make sure that $resellers array has a value in that position. i.e:
if $reseller array looks like this:
array(3) { [0]=> string(5) "first" [1]=> string(6) "second" [2]=> string(5) "third" } string(5)
and GD_RESELLER constant equels '2', then asking for
$resellers[GD_RESELLER]
will return 'third'
remember that arrays in PHP use zero-based index,
so the first child of that array is in position '0' and not '1', etc.
See PHP's Docs about Arrays
Another simple solution, just edit bellow code in gd-config.php file.
define( 'GD_RESELLER', false);
I'm trying to do the following in my controller:
public function moveMessagesToArchive( $message_ids = Input::get('message_ids') )
{
$json = json_encode( $message_ids);
echo $json;
}
And it keeps throwing the following error:
syntax error, unexpected '(', expecting ')'
at the function signature. What's causing the problem here?
Update
While, I wait for the reason I've written the following work around:
public function moveMessagesToArchive( $message_ids = array() )
{
$ids = array();
if ( ( count($message_ids) !== 0 ) && is_array($message_ids) ) {
$ids = $message_ids;
} else if ( Input::get('message_ids') ) {
$ids = Input::get('message_ids');
} else {
return false;
}
$json = json_encode( $ids );
echo $json;
}
It is not possible in PHP. According to the documentation
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
Reference Example 4
What you are trying to do is not, and never has been supported by php.
Nothing at all to do with Laravel.
I get the following error on my WP Dashboard:
Warning: First parameter must either be an object or the name of an existing class
in /home/content/88/11746388/html/wp-content/plugins/pluginname/pluginname.php on
line 136
Those are my lines from 130 to 140. Any ideas?
function get_latest_version($plugin_file) {
$latest_version = '';
$sr_plugin_info = get_site_transient ( 'update_plugins' );
// if ( property_exists($sr_plugin_info, 'response [$plugin_file]') && property_exists('response [$plugin_file]', 'new_version') ) {
if ( property_exists($sr_plugin_info, 'response [$plugin_file]') ) {
$latest_version = $sr_plugin_info->response [$plugin_file]->new_version;
}
return $latest_version;
}
it seems that you have error in this line :
if ( property_exists($sr_plugin_info, 'response [$plugin_file]') ) {
property_exists — Checks if the object or class has a property .
and 'response [$plugin_file]' it is not a valid property name of class.
To be honest that code is bleeding from several wounds.
a.) get_site_transient is not guaranteed to return an object and property_exists expects one
b.) you try to check if an array key exists in a property with property_exists. I'm fairly certain that property_exists can not do that - you should only check for response property with it and use isset() for the $plugin_file index.
function get_latest_version($plugin_file) {
$latest_version = '';
$sr_plugin_info = get_site_transient ( 'update_plugins' );
// if ( property_exists($sr_plugin_info, response[$plugin_file]) && property_exists(response[$plugin_file], 'new_version') ) {
if ( property_exists($sr_plugin_info, response[$plugin_file]) ) {
$latest_version = $sr_plugin_info->response[$plugin_file]->new_version;
}
return $latest_version;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
default method argument with class property?
I'm writing a recursive function and just for ease of use I want the first call of the function to accept a default argument. This value has to be the address of an object member variable. See below full code:
class Test{
public $hierarchy = array( );
public function addPath( $path, &$hierarchy ){
$dirs = explode( '/', $path );
if( count( $dirs ) == 1 ){
if( is_dir( $path ) )
$hierarchy[ $dirs[ 0 ] ] = '';
else
$hierarchy[ $path ] = '';
return $hierarchy;
}
$pop = array_shift( $dirs );
$hierarchy[ $pop ] = $this->addPath(
implode( '/', $dirs ), $hirearchy[ $pop ] );
return $hierarchy;
}
}
$t = new Test( );
$t->addPath( '_inc/test/sgsg', $t->hierarchy );
print_r( $t->hierarchy );
Now, what I would like to do here ideally is add a default value:
public function addPath( $path, &$hierarchy = $this->hierarchy ){
So that I can call it like this:
$t->addPath( '_inc/test/sgsg' );
But this gives me the following error:
Parse error: syntax error, unexpected '$this' (T_VARIABLE) in tst.php on line 9
I've been trying a few things with no success. Is there any way I can achieve this?
Unfortunately you cannot do this, the parser does not (cannot!) cater for resolving variables in function definitions.
You can however define the function with &$hierarchy = null in the definition and use is_null to see if a value was passed. (unless your referenced value will sometimes be null, then you'll need another workaround)
If is_null returns true then you can assign $hierarchy = &$this->hierarchy
After a quick discussion in PHP chat, using func_num_args() might be useful here. It doesn't count args that are populated with defaults, so you can safely pass variables that contain null by reference, and use this function to determine if the value in $hierarchy came from a passed parameter, or by the default. (thanks #NikiC)