Object property's value is an array. Getting notice/warning saying otherwise - php

I'm getting a Notice and a Warning here, not an error. My code still works. Just wanted to see if someone could figure out why I'm getting the notice and warning.
Notice: Trying to get property of non-object in file.php on line 152
Warning: in_array() expects parameter 2 to be array, null given file.php on line 152
Line 152 is an if() statement:
if($user->type == 'x' && in_array($user->email, $campaign->settings->email_list))
{ do stuff }
I've pinpointed the issue to the $campaign object. Using print_r on $campaign outputs quite a bit of info, but this is the important part:
Campaign Object
{
[settings] => stdClass Object
(
[email_list] => Array
(
[0] => support#domain.com
[1] => customer#domain.com
)
)
}
Obviously, $campaign->settings->email_list is an array. Why am I getting the Notice and Warning, then? $campaign is created directly above Line 152.

I think you may get this notice because of possible uninitialization (maybe null value) assigned to $campaign->settings campaign's internal object.
Please ensure with a var_dump($campaign->settings) what is the real value of the property before the line with in_array function.

try this some time it will works.
$campaign['settings']['email_list']

Related

end() expects parameter 1 to be array - php

I'm retrieving the email list from a MySQL database along side with the IDs of the users it gets something like this
Array (
[0] => Array ( [ID] => 1 [Email] => email1 )
[1] => Array ( [ID] => 2 [Email] => email2 )
)
and while trying to test for the value of the last email "email2" I used
end(end($array_sample));
this used to work on my old server running PHP 5.0 and stopped at the new one running PHP 5.6
Was there something I did wrong or is it a php version?
I basically changed the whole approach to get the site to do what it was meant to do any how, but I still would like to learn about the end(end(array)) issue.
end() function needs to get array by reference, so it can't be a result of other function, because you get following error:
Only variables should be passed by reference
To avoid it assign result of inner end() to variable and then use end() on this variable:
$tmp = end($array);
$result = end($tmp);
And you probably don't get any error in previous version of PHP due to error_reporting set to quiet them.
According to documentation:
Prior to PHP 5.4.0 E_STRICT was not included within E_ALL, so you
would have to explicitly enable this kind of error level in PHP <
5.4.0.
As far as I know, your code should had never worked:
Only variables should be passed by reference
As documentation explains:
This array is passed by reference because it is modified by the
function. This means you must pass it a real variable and not a
function returning an array because only actual variables may be
passed by reference.
What has changed is the severity of the error. It has been a fatal error, a strict standards notice and a regular notice. Between 4.3.0 and 5.0.4 is just failed silently.
Most likely the error went unnoticed until you upgraded and an actual error message was triggered.
You have an end within another end, the inner one returns last element of array, the outer one is expecting an array not a single value

PHP, getting Undefined property: stdClass::$id but it definitely exists

So it's really simple code and I am yanking my hair out with the problem.
//I first retrieve some JSON info (confirmed to work fine)
$file=file_get_contents('url');
//I then decode and print to verify (still working)
$somename=json_decode($file);
I print it out just to make sure it works (it does):
print_r($somename);
The print out reads as follows:
stdClass Object ( [id] => 456456456 [name] => somename [Stuff01] => 55 [Stuff02] => 25 [Stuff03] => 123132123132 ) )
Now I just want to get the value in the 'id' key so I use the appropriate object call:
$thisID=$somename->{'id'};
But I get the error:
Notice: Undefined property: stdClass::$id
I print_r every time so I know it's there. I can see it. What am I doing wrong?
I have had no problems doing this exact thing many times.
How do you access to individual properties is based on your data structure. You have kind of nested structure, object in object. Try like this:
$somename->somename->id;
//or
$yourObjectName->somename->id;
I hope this helps!

array_keys error while running artisan basset

I am receiving the proceeding error exception when I run php artisan basset --tidy-up from the command line.
[ErrorException]
array_keys() expects parameter 1 to be array, object given
basset [--delete-manifest] [--tidy-up]
I was unfortunate in finding any details online.
Although I've not used Basset, the reason for this error is that you are passing in an object, rather than an array, for the first argument of array_keys.
For example, were I to have an array as follows:
$myGreatArray = array('first' => 'foo', 'bar' => 'sup');
And then pass it in to array keys:
print_r(array_keys($myGreatArray));
I'd get this as the output:
Array
(
[0] => first
[1] => bar
)
However, I can't do the same thing with an object, even if it's structured the same way.
In other words, we can't do something like this (and not just because you need to var_dump objects, but also because you need to pass in arrays to array_keys()):
$myGreatArray = new stdClass;
$myGreatArray->first = 'foo';
$myGreatArray->bar = 'sup';
print_r(array_keys($myGreatArray));
You'll get an error similar to what you're seeing, although what you're seeing appears to be in a format of an exception handled by Basset. You can also see it when running a script from other than the command line, depending on your level of error reporting.
Edit:
I just pulled up the source for Basset's command line script, and it looks like you're getting the error on line 120, which reads as follows:
$collections = array_keys($this->environment->all()) + array_keys($this->manifest->all());
You might want to check the way your manifest and environment are structured.

PHP - Function name from variable

Okay, I've found a possible solution for this, but for some reason, I can't make it work in my application. Apparently, if I have a variable which contains a name function, I could use
<?php echo $variable(); ?>
to output the function with the same name.
I'm using Codeigniter. It has a function in its Form helper to output a text field, which is
<?php form_input(); ?>
I have a variable
<?php $instance['taxon_field'] = 'form_input'; ?>
If I echo out this variable, I do get the needed value, 'form_input'. However, as soon as I try to echo
$instance['taxon_field']()
I get a warning:
Message: Illegal string offset 'taxon_field'
and a fatal error:
Call to undefined function p()
I am really clueless here, because echoing only the variable gives 'form_input', but echoing $variable() only gives 'p'.
Where am I doing wrong?
The actual problem here is that $instance is not an array, but a string. Judging from the error message, it's a string whose value starts with p.
The syntax $var[$key] is used not only to access array elements but also to index into strings, where $var[0] would be the first character (actually, byte) of $var etc. If $instance is a string and you write $instance['taxon_field'] then PHP will try to convert 'taxon_field' to an integer in order to index into the string. This results in 0 as per the usual conversion rules, so the whole expression gets you the first letter of the string.
Assuming that the string starts with p it's then pretty obvious why it tries to call a function with that name.
Use call_user_func()
call_user_func($instance['taxon_field']);
The confusion created is actually my own fault because I failed to provide some aditional information which I thought was not important, but turned out to be crutial. My $instance[] array is actually a result of a foreach loop (two of them, to be precise) and is a part of a bigger multidimensional array. The actual code is more complicated, but I'll try to represent it right:
<?php
$bigger_array = array(
0 => array(
'field_one' => 'value_one',
'field_two' => 'value_two',
'field_three' => 'new_function'
),
1 => array(
'field_one' => 'new_value_one',
'field_two' => 'new_value_two',
'field_three' => 'echo'
)
);
function new_function()
{
echo 'New function called.';
}
foreach($bigger_array as $instance)
{
$name = $instance['field_three'];
$name('Hello World!');
}
?>
This will output the following:
New function called.
Fatal error: Call to undefined function echo() in /opt/lampp/htdocs/bla.php on line 69
In other words, the newly defined function works fine, but the built-in 'echo' doesn't.
This is actually not my original problem, this is something that I've encountered while trying to debug the initial issue. And the original problem is that creating a function from a single-dimensional array works okay. whereas creating a function from a multi-dimensional array within a foreach loop transforms the array into a string with the value of its last member.
Now, I'm still not really able to fully answer my question, but I think information I'm giving could lead to a solution. In the simplified example that I gave here, why am I getting the message that echo() function is not defined, while the new function works fine?

"Trying to get property of non-object" error with SimpleXML and PHP

I'm using a PHP script with SimpleXML to parse an XML feed. I have no control over the content of the XML.
try { $xml = #new SimpleXMLElement($fetchResult); } catch (Exception $e) { errorHandle($e->getMessage());}
$userNick = $xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->HBoxView->TextView->SetFontStyle->b;
foreach ($xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->VBoxView as $pathToSubTree){
foreach ($pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView as $canopy){
//Do some stuff now that we've found the canopy of the tree
}
$canopy = $pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView;
if(is_null($canopy)){
//Do some stuff stuff is the canopy was not traceable
}
}
$pathToSubTree = $xml->View->ScrollView->VBoxView->View->MatrixView->VBoxView[0]->VBoxView;
if(is_null($pathToSubTree)){
//Do some stuff stuff is the subTree path was not traceable
}
unset($xml);
I'm getting lots of two errors, which I'm sure are related to the same cause:
PHP Notice: Trying to get property of non-object in myScript.php on line 45
PHP Warning: Invalid argument supplied for foreach() in myScript.php on line 45
PHP Notice: Trying to get property of non-object in myScript.php on line 76
Line 45 is (from above):
foreach ($pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView as $canopy){
Line 76 is (from above):
$canopy = $pathToSubTree->MatrixView->View->VBoxView->VBoxView->HBoxView[0]->VBoxView->MatrixView->VBoxView;
I'm pretty sure this error is caused by one of the arrays in my path not being an array for the particular XML, but some times it CAN be an array.
What's the correct way to deal with these?
for tags can appear multiple times or singularly:
is_array($doc->node) ? $doc->node[0] : $doc->node
or it might be easier to use:
$node->xpath('MatrixView/View/XBoxView/VBoxView/HBoxView[1]/VBoxView/MatrixView/VBoxView)
the [1] is the first element matched
Here's an explanation of those error messages:
PHP Warning: Invalid argument supplied for foreach() in myScript.php on line 45
This one is easy. You've passed something that couldn't be iterated over to foreach, such as foreach (false as $x).
In your case, your crazy-long series of $foo->bar->baz probably returns null because the element doesn't exist.
PHP Notice: Trying to get property of non-object in myScript.php on line 45
"Trying to get property" you surely know that an object's property "bar" is written ->bar, "of non-object" refers to the variable right before the ->. It means that somewhere in your $xml->View->ScrollView->... there's an element that doesn't exists and SimpleXML returns null. In consequence the next -> triggers that error.
In short, your "path" to the element is wrong.

Categories