Creating default object from empty value in php 5.4 - php

i have some code like this:
function getAuthorizedPFComponents($pfState)
{
$authorizedPFComponents = new \stdClass();
$compTypeMap=array('platform'=>'pfAuthorizations','mainSite'=>'mainSiteAuthorizations','microSites'=>'microSiteAuthorizations','apps'=>'appAuthorizations');
foreach($compTypeMap as $compType=>$tagName)
{
$authorizationsNode=$this->pfAuthXMLDOM->getElementsByTagName($tagName)->item(0);
foreach($authorizationsNode->getElementsByTagName('authorizations') as $pfComponentAuthElem)
{
foreach($pfComponentAuthElem->getElementsByTagName('allow') as $allow)
{
switch($allow->getAttribute('orgCode'))
{
case 'K_ALL':
{
$authorizedPFComponents->$compType->{$pfComponentAuthElem->getAttribute('pfComponentCode')}->storeCode=$allow->getAttribute('storeCode');
}
}
It shows a warning:
Warning: Creating default object from empty value
The warning is traced back to the code under case K_ALL:

This was too long for a mere comment, so I'll remove it when it has outlived its usefulness.
First thing you should do is make the code simpler; there's a lot of stuff going on in that one statement:
$compCode = $pfComponentAuthElem->getAttribute('pfComponentCode');
$storeCode = $allow->getAttribute('storeCode');
And add debug code:
var_dump($authorizedPFComponents->$compType);
var_dump($authorizedPFComponents->$compType->$compCode);
$authorizedPFComponents->$compType->$compCode->storeCode = $storeCode;
Consider this code:
$x->y = 'test';
If $x is not defined, it will issue a warning:
Warning: Creating default object from empty value in xxx
The same goes for your chain of references:
$authorizedPFComponents->$compType->$compCode->storeCode
If any of those paths is empty, the next ->yyy will cause that warning.

Related

Guide a newbie: PHP 7 warning: Warning: count(): Parameter must be an array or an object that implements Countable

After upgrading to PHP 7 I get the following error message in a really old WP theme. Unfortunately changing the theme is not an option right now, so I have to try to find a fix. I can kind of read PHP, but have a hard time writing it on my own, so I'd love some pointers on how to change this.
The Error message:
Warning: count(): Parameter must be an array or an object that implements Countable
The code that needs to implement Countable
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE) {
$settings = $this['option']->get('warp_theme_options', array());
} else {
$settings = ($file = $this['path']->path('template:config')) ? file_get_contents($file) : array();
}
// set config or load defaults
if (count($settings)) {
$this->config = $this['data']->create($settings);
} else {
$this->config = $this['data']->create(file_get_contents($this['path']->path('template:config.default')));
}
From trying to read up on this I think the error is that count no longer allows Null? But how would I change the code if statement above to work then?

Changing module name

I am customising a opensource phreebooks. I want to change the module names in that. I did change the module names for phreebooks and phreedom successfully, but when i change the name for phreedom i can not print any pdf. I am getting this error
User: 1 Company: phree RUN-TIME WARNING: 'Creating default object from empty value' line 50 in file
F:\wamp\www\phree\modules\report\pages\popup_gen\pre_process.php
In line no 50, code is like this
if (isset($_GET['xfld'])) $report->xfilterlist[0]->fieldname = $_GET['xfld'];
Can somebody please tell me where exactly i am doing wrong. I have been tying it from past 3 days.
EDITED
if (isset($_GET['xfld'])) { // check for extra filters
if (!isset($_GET['xfld'])) $xfld = new stdClass();
echo "BLANK";
if (isset($_GET['xfld'])) $report->xfilterlist[0]->fieldname = $_GET['xfld'];
if (isset($_GET['xcr'])) $report->xfilterlist[0]->default = $_GET['xcr'];
if (isset($_GET['xmin'])) $report->xfilterlist[0]->min_val = $_GET['xmin'];
if (isset($_GET['xmax'])) $report->xfilterlist[0]->max_val = $_GET['xmax'];
}
i did like this
It displays "BLANK" but actually isset($_GET['xfld'] is SET from URL.
$_GET['xfld'] = $xfld;
$xfld = NULL;
$xfld->success = false; // Warning: Creating default object from empty value
PHP will report a different error message if $xfld is already initialized to some value but is not an object:
$xfld = something;
$xfld->success = false; // Warning: Attempt to assign property of non-object
You shoud check whether the object already exists:
if (!isset($_GET['xfld'])) $xfld = new stdClass();
Otherwise, this code is not an equivalent replacement for the "old PHP" implicit object creation.

PHP error on testing server

I have a bit of PHP code that works fine on my production server but not on my test server. Here is the code:
function callProcedure0(&$connection, $procname, $dofunction)
{
mysqli_multi_query($connection, "CALL " . $procname . "();");
$first_result = 1;
do
{
$query_result = mysqli_store_result($connection);
if ($first_result)
{
$dofunction($query_result);
$first_result = 0;
}
if ($query_result)
{
mysqli_free_result($query_result);
}
$query_result = NULL;
} while(mysqli_next_result($connection));
}
...
function doGenres($in_result)
{
global $genre_array, $game_array, $genre_order_array;
$genre_count = 1;
// foreach is necessary when retrieving values since gaps may appear in the primary key
while ($genre_row = mysqli_fetch_row($in_result)) // line 81 is here!
{
$genre_array[] = $genre_row[0];
$genre_order_array[$genre_row[1] - 1] = $genre_count;
$game_array[] = [[],[]];
$genre_count += 1;
}
}
...
callProcedure0($con, "get_genres_front", doGenres); // line 138 is here!
The "get_genres_front" bit refers to a stored procedure on my database. Here are the errors:
Notice: Use of undefined constant doGenres - assumed 'doGenres' in /opt/lampp/htdocs/keyboard/keyboard.php on line 138
Again, there are no problems on the production server which is using Apache 2.2.23, MySQL 5.1.73-cll, PHP 5.4.26. The test server where things are broken is running Apache 2.4.10, MySQL 5.6.21, PHP 5.5.19.
Did something change in recent software versions? Thanks.
[edit]
This is not a duplicate question. I'm worried about the first error. I already know what to do about the second error which I have deleted.
The code you have posted is wrong, you must pass function name as string and then use call_user_func to invoke this function.
In your callProcedure0 function change
$dofunction($query_result);
to
call_user_func($dofunction, $query_result);
And then call it with the function name as string like this
callProcedure0($con, "get_genres_front", "doGenres");
The above code could work also with invoking the function with
$dofunction($query_result);
on some php versions, but the line where you pass the function name it should be string, otherwise PHP assumes it is a constant.

PHP Array breaking

This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program
The code is
$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);
This line:
$oldFileArray[$key] = $bufflerArray[1];
Is throwing out this error
Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57
I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.
I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?
Thanks,
Stephen.
add checks for empty
if (!empty($bufflerArray[1])) {
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}

Why printing config array gives empty Array?

$tpl = new Smarty();
$tpl->configLoad('compose.conf');
$config = $tpl->getConfigVars();
print_r($config);
it returns
Array()
What is it, I am doing wrong?
compose.conf
[jquery]
jquery = lib/jquery/jquery.js
[jquery_validate]
css=res/css/jquery.validate.css
js=lib/jquery/jquery.validate.js
X=jquery
[bootstrap_css]
main = lib/bootstrap/css/bootstrap.min.css
theme = lib/ootstrap-theme.min.css
[bootstrap_js]
js = lib/bootstrap/js/bootstrap.min.js
X=jquery
[bootstrap]
X=bootstrap_css,bootstrap_js
[utils]
utils=lib/utils/utils.js
odo=lib/utils/utils.odo.js
require=libutils/utils.require.js
template=lib/utils/utils.template.js
X=jquery
In your smarty plugins directory
smarty_internal_config.php
search for statment
if (!empty($sections)) {
now replace this statement with
if($sections=='*'){
foreach ($_config_vars['sections'] as $key=>$value) {
if (isset($value['vars'])) {
$scope_ptr->config_vars[$key] = $value['vars'];
}
}
} else if (!empty($sections)) {
and while loading file use it like this
$tpl = new Smarty();
$tpl->configLoad('compose.conf','*');
$config = $tpl->getConfigVars();
print_r($config);
Thats it:)
According to the manual of parse_ini_file:
Note: There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, >>> on <<<, off, none. Values null, off, no and false result in "". Values on, yes and true result in "1". Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value.
If we try to execute parse_ini_file (or parse_ini_string) on your file we get the following error:
Warning: syntax error, unexpected BOOL_TRUE in Unknown on line 7
in /tmp/execpad-e67cf6f095ae/source-e67cf6f095ae on line 32
Hence Smarty is getting an error (I'm assuming that it uses one of these functions internally) when trying to parse your INI file because you use a reserved word. The solution is to simply rename ON to something else.
Update:
Smarty doesn't use these functions, but its parser replicates it. Line #313 of smarty_internal_configfilelexer.php refers to "on":
if (!$this->smarty->config_booleanize || !in_array(strtolower($this->value), Array("true", "false", "on", "off", "yes", "no")) ) {
// ^^

Categories