Illegal String Offset 'class' in PHP - php

I received this warning
Warning: Illegal string offset 'class' in C:\xampp\htdocs\myweb\libraries\cms\html\html.php on line 971
Warning: Illegal string offset 'class' in C:\xampp\htdocs\myweb\libraries\cms\html\html.php on line 972
Warning: Illegal string offset 'class' in C:\xampp\htdocs\myweb\libraries\cms\html\html.php on line 972
On joomla 3.3 (file path \libraries\cms\html\html.php) and the code is:
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null)
{
static $done;
if ($done === null)
{
$done = array();
}
$attribs['class'] = isset($attribs['class']) ? $attribs['class'] : 'input-medium';//happen here
$attribs['class'] = trim($attribs['class'] . ' hasTooltip');//happen here
$readonly = isset($attribs['readonly']) && $attribs['readonly'] == 'readonly';
$disabled = isset($attribs['disabled']) && $attribs['disabled'] == 'disabled';
if (is_array($attribs))
{
$attribs = JArrayHelper::toString($attribs);
}
.......
Shows that it had to do with $attribs['class']. And if I'm correct illegal string offset could mean that $attribs is not an array but a string. So is there any way to correct this?
I'm on PHP5.4

This:
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = null)
{
}
Should be:
public static function calendar($value, $name, $id, $format = '%Y-%m-%d', $attribs = array())
{
}
Point is that by setting "null" to the variable by default, you actually say it's an empty string, or an empty variable. By setting "array()", you define the variable being an empty array.
Just by looking a little bit further, there is an array expected, since the code is looking for specific array_keys like: 'readonly' AND 'disabled'.

Related

Illegal offset type in my php class function

I am getting illegal offset error in one of my function.
Here is the code
public function translate($str, $namespace = '') {
$namespace = $this->getNamespace($namespace);
$this->load($namespace);
$lang = $this->getLang();
$data = $this->data[$namespace];
$key = md5($str);
if (isset($data[$lang][$key])) {
return $data[$lang][$key];
}
$default = $this->getDefaultLang();
if ($lang == $default) {
if ($this->getSaveOnMissing()) {
$save = array(
'namespace' => $namespace,
'lang' => $lang,
'trans_key' => $key,
'trans_val' => $str
);
$sql = $GLOBALS['db']->strInsert('translate', $save);
if ($GLOBALS['db']->SQL_query($sql)) {
$this->data[$namespace][$lang][$key] = $str;
}
}
return $str;
} elseif (isset($data[$default][$key])) {
return $data[$default][$key];
}
return $str;
}
In this line I am getting this error
elseif (isset($data[$default][$key]))
So there are 3 levels of your value
$data
$default
$key
You can check by three levels first if they are all returning something
elseif(isset($data, $data[$default], $data[$default][$key])

2 warnings: Cannot use a scalar value as an array and Illegal offset type in isset or empty

Here is the code that I wrote:
class init{
private $time_spent;
public function __construct(){
$this->time_spent = array(); //<- caching variable
}
function get_course_unit_time_spent( $user_id,$course_id,$unit_id ){
if(empty($this->time_spent) || empty($this->time_spent[$course_id]) || empty($this->time_spent[$course_id][$unit_id])){
$this->time_spent[$course_id][$unit_id] = get_user_meta($user_id,'time_spent_'.$course_id.'_'.$unit_id,true);
if( empty($this->time_spent[$course_id][$unit_id]) ){
$this->time_spent[$course_id][$unit_id] = 0;
}
}
return $this->time_spent[$course_id][$unit_id];
}
function get_course_time_spent( $user_id,$course_id ){
if( empty($this->time_spent[$course_id]) ){
if(!bp_course_is_member($course_id,$user_id))
return 0;
$time_spent = 0;
$this->time_spent[$course_id] = array();
$course_curriculum = bp_course_get_curriculum( $course_id );
foreach ($course_curriculum as $key => $unit_id) {
if( is_numeric($unit_id) ){
$time_spent += $this->get_course_unit_time_spent( $user_id,$course_id,$unit_id );
}
}
$this->time_spent[$course_id] = $time_spent;
}
return $this->time_spent[$course_id];
}
}
There are lots of code but the issue is with the first function I wrote above, in two different places I am getting the above to warnings.
1) Warning: Cannot use a scalar value as an array on line 10 and 12 (in the above code).
2) Warning: Illegal offset type in isset or empty on line 8 (in the above code).
3) Warning: Illegal offset type on line 10, 11 and 12 (in the above code).
The second function is not throwing any error but whenever I use the 1st function or the second everytime the warning comes from the 1st function. I am not sure what is it, can someone help to correct it ?
UPDATE: Updated the code and now the first warning is gone but still getting the illegal ofset error.
Your calling get_course_time_spent(), which creates an array for
$this->time_spent[$course_id][$unit_id] in
get_course_unit_time_spent()
Then your setting $this->time_spent[$course_id] as an int once the foreach loop has finished.
Then on the next call to get_course_unit_time_spent your checking
as if its an array empty($this->time_spent[$course_id][$unit_id])

PHP ReflectionMethod does not get default boolean value of param

When I try to get the value of a boolean param, with ReflectionMethod, that have a default value set, I got empty result.
With this code:
public function GetOrderBook($symbol = null, $limit = 100, $async = false)
{
if ($symbol !== null) {
$params = [];
$ref = new \ReflectionMethod($this, 'GetOrderBook');
foreach ($ref->getParameters() as $param) {
$name = $param->name;
$params[$name] = $$name;
}
print_r($params);
}
}
I get this:
Array (
[symbol] => ETHBTC
[limit] => 100
[async] =>
)
Is there a way to get the default value of a param with reflection?
print_r function outputs string representation of values. String representation of false is empty string. To see real values that you have in an array, use var_dump:
var_dump($params);
After that you will see that:
["async"]=>bool(false)

Illegal string offset with php 7.1.6

This code throws a waring under PHP 7.1.6... Under PHP 5.x.x it does not have any problems.
The offending line is $attributes['onclick'] = $onclick;, with the warning Illegal string offset 'onclick'.
Here is my code:
protected function js_anchor($title, $onclick = '', $attributes = '')
{
if ($onclick)
{
$attributes['onclick'] = $onclick;
}
if ($attributes)
{
$attributes = _parse_attributes($attributes);
}
return '<a href="javascript:void(0);"'.$attributes.'>'.$title.'</a>';
}
$attributes is initialized as an empty string. You need to make it an empty array, $attributes = []
protected function js_anchor($title, $onclick = '', $attributes = [])
{
if ($onclick)
{
$attributes['onclick'] = $onclick;
}
if ($attributes)
{
$attributes = _parse_attributes($attributes);
}
return '<a href="javascript:void(0);"'.$attributes.'>'.$title.'</a>';
}

Using variable variables to populate array results in error

I have the following code:
function filterUsers(array $setOfAllUsers) {
if (empty($setOfAllUsers)) {
return array(array(), array());
}
$activeUsers = array();
$inactiveUsers = array();
foreach($setOfAllUsers as $userRow) {
$var = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';
$$var[$userRow['CID']]['Label'] = $userRow['UserLabel'];
// Error happens here ---^
$$var[$userRow['CID']]['UserList'][$userRow['UID']] = array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
... More data
);
}
return array($activeUsers, $inactiveUsers);
}
I get the following error: Warning: Illegal string offset 'Label' in ...
How can I fix this? I tried defining Label part first like this: $$var[$userRow['CID']] = array(); $$var[$userRow['CID']]['Label'] = ''; but did not work.
To make things clear what I am trying to achieve is this:
if ($userRow['IsActive']) {
$activeUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];
$activeUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
... More data
);
} else {
$inactiveUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];
$inactiveUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
... More data
);
}
Instead of repeating above in if/else I wanted to achieve it using $$
Try using ${$var} instead of $$var.
EDIT
From PHP Manual (http://php.net/manual/en/language.variables.variable.php):
In order to use variable variables with arrays, you have to resolve an
ambiguity problem. That is, if you write $$a[1] then the parser needs
to know if you meant to use $a[1] as a variable, or if you wanted $$a
as the variable and then the [1] index from that variable. The syntax
for resolving this ambiguity is: ${$a[1]} for the first case and
${$a}[1] for the second.
function filterUsers(array $setOfAllUsers) {
if (empty($setOfAllUsers)) {
return array(array(), array());
}
$users = array(
'inactiveUsers' => array(),
'activeUsers' => array()
);
foreach($setOfAllUsers as $userRow) {
$status = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';
$users[$status][$userRow['CID']] = array();
$users[$status][$userRow['CID']]['Label'] = $userRow['UserLabel'];
$users[$status][$userRow['CID']]['UserList'] = array(
$userRow['UID'] => array(
'FirstName' => $userRow['FName'],
'LastName' => $userRow['LName'],
)
);
}
return $users;
}
Try the following:
$myUsers = $$var;
$myUsers[...][...] = ...;
The problem with using $$var[...][...] is that first $var[...][...] is evaluated and then it tries to find the variable named $$var[...][...].
Don't use var-vars like this. You run into a PHP syntax glitch. And even if there wasn't a syntax glitch, you shouldn't be using var-vars in the first place. They make for near-impossible-to-debug code.
$x = 'foo';
$foo = array();
$$x[0] = 1;
var_dump($x); // string(3) "foo"
var_dump($foo); // array(0) { }
$$x[1][2] = 3;
PHP Notice: Uninitialized string offset: 2 in php shell code on line 1
Note how the array didn't get modified by the $$x[0] assignment, and how the 2+ dimensional assigment causes "undefined string offset". Your var-var isn't being treated as an array - it's being treated as a STRING, and failing, because it's a syntax glitch. You can treat strings as arrays, but not using var vars:
$bar = 'baz';
$bar[1] = 'q';
echo $bar; // output: bqz
There appears to be NO way to use a var-var as an array, especially a multi-dimensional array.

Categories