SYMFONY4 : "Notice: Undefined variable: parentClass" - php

I've got this error message when i try to go to a page which calls an entity. (For example the contact page, or articles page)
"Notice: Undefined variable: parentClass" which refer me to this file
vendor\doctrine\persistence\lib\Doctrine\Common\Persistence\AbstractManagerRegistry.php line 176
public function getManagerForClass($class)
{
// Check for namespace alias
if (strpos($class, ':') !== false) {
[$namespaceAlias, $simpleClassName] = explode(':', $class, 2);
$class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
}
$proxyClass = new ReflectionClass($class);
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
$parentClass = $proxyClass->getParentClass();
if (! $parentClass) {
return null;
}
}
$class = $parentClass->getName();
foreach ($this->managers as $id) {
$manager = $this->getService($id);
if (! $manager->getMetadataFactory()->isTransient($class)) {
return $manager;
}
}
}
I really dont know what to do.
If someone got an idea or already had this error :)
Thank you !

your correctly indented code looks like this:
$proxyClass = new ReflectionClass($class);
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
$parentClass = $proxyClass->getParentClass();
if (! $parentClass) {
return null;
}
}
$class = $parentClass->getName();
now, in the case that $proxyClass->implementsInterface(...) is FALSE there is no $parentClass defined, hence the error message.
since the code appears to be in doctrine, maybe it's a bug. Maybe it was fixed and you use an old version for some reason. maybe update?
in the current version the line is inside the if-block

you're absolutely right, i don't know why $class = $parentClass->getName(); was out the 'if', the problem was effectively this ! Thank you !

Related

Exception Serialization of 'Closure' is not allowed

I am trying to cache a sitemap generated from a controller for a website that I am working on, but apparently I am doing something wrong, as I don't understand the Error message.
Here is the code snippet causing the trouble (it is a controller Method). Everything works correctly until I add the caching.
public function mapContent($type, Request $request)
{
$cachingKey = $request->fullUrl();
if ($type !== 'news' && $type !== 'pages') {
abort(404);
} elseif (Cache::has($cachingKey)) {
$items = Cache::get($cachingKey);
} else {
$items = $this->_getElementsSitemap($type);
Cache::put($cachingKey, $items, now()->addDays(7));
}
return Response::make($items, '200')->header('Content-Type', 'application/xml');
}
Seems that $items = $this->_getElementsSitemap($type); returns not serializable instance.
Your class should implement __serialize method

Trying to get property of non-object Notice in custom avatar function for Wordpress

The following custom avatar functions works fine, but I get the following notice
"Trying to get property of non-object in..."
The notice says the problem is on the last part of the function I pasted here - I marked it in the code (look for <-- Notice mentions this line)
Any idea how to fix this? I am stuck...
function test_get_avatar($avatar, $id_or_email, $size, $default, $alt) {
if (!is_numeric($id_or_email)) {
if (is_string($id_or_email)) {
$user = get_user_by('email', $id_or_email);
$id_or_email = $user->ID;
} else if (is_object($id_or_email)) {
if (!empty($id_or_email->ID)) {
$id_or_email = $id_or_email->ID;
}
if (!empty( $id_or_email->comment_author_email)) {
$user = get_user_by('email', $id_or_email->comment_author_email);
$id_or_email = $user->ID; <-- Notice mentions this line
}
}
}
$avatar_id = get_user_meta($id_or_email, 'nicobartes_user_avatar', true);
...
Yes, because get_user_by() can fail and return false. At that point you won't have a wp user object. A test around this code would be:
if ($user = get_user_by('email', $id_or_email->comment_author_email)) {
$id_or_email = $user->ID;
} else {
//Whatever you want to do when this lookup fails
$id_or_email = 0;
}
When you run
$user = get_user_by('email', $id_or_email->comment_author_email);
You should check the value of $user before attempting to get the id on the next line. According to the Wordpress docs, it could potentially be false if no user is found.

PHP (Laravel) issue

I am receiving an error from running the below code?
(1/1) ErrorException
Creating default object from empty value
Code:
public function setServiceSetting(Request $request) {
if (!$request->has('setting_key') ||
!$request->has('setting_value')) {
return $this->getScriptingResponse();
}
$settingKey = $request->input('setting_key');
$settingValue = $request->input('setting_value');
$settings = WebsiteSettings::first();
if ($settings == null) {
return;
}
$setting->$settingKey = $settingValue;
$settings->save();
}
Error Line:
$setting->$settingKey = $settingValue;
I know the column exists, below anyone suggests that. I also know both parameters are non-null.
I think that this is incorrect
$settings->$settingKey = $settingValue;
try this:
$settings->settingKey = $settingValue;

CakePHP - call_user_func_array() looses $this->request->data?

I need to call Controller-methods dynamically. For this purpose I wrote the following code:
if(isset($method['postParams'])) {
foreach($method['postParams'] as $index => $param) {
$this->request->data[ $index ] = $param;
}
}
App::import('Controller', $method['controller']);
// get result
$method['controller'] = $method['controller'] . 'Controller';
$Controller = new $method['controller']();
try {
if(count($method['params'])) {
$varVal = call_user_func_array(array($Controller, $method['method']), $method['params']);
} else {
$varVal = call_user_func(array($Controller, $method['method']));
}
} catch(Exception $e) {
$varVal = $e;
}
$this->set($varName, $varVal);
Now I debug the function being called and see that $this->request->data is NULL.
How to solve this?
For the sake of completeness I'll post the answer to this problem for anyone having a similar issue to find the solution more easily.
Instead of:
$this->request->data
Use:
$Controller->request->data
After instanciating the $Controller object.

Error When Calling Function

I searched forever trying to find an answer, but was ultimately stumped. I've been writing code to allow multiple bots to connect to a chat box. I wrote all the main code and checked it over to make sure it was all okay. Then when I got to calling the function needed to make it work, it gave me an error saying:
Notice: Undefined variable: ip in C:\wamp\www\BotRaid.php on line 40
And also an error saying:
Fatal Error: Cannot access empty property in C:\wamp\www\BotRaid.php
on line 40
( Also a screenshot here: http://prntscr.com/ckz55 )
<?php
date_default_timezone_set("UCT");
declare(ticks=1);
set_time_limit(0);
class BotRaid
{
public $ip="174.36.242.26";
public $port=10038;
public $soc = null;
public $packet = array();
##############################
# You can edit below this #
##############################
public $roomid="155470742";
public $userid = "606657406";
public $k = "2485599605";
public $name="";
public $avatar=;
public $homepage="";
##############################
# Stop editing #
##############################
public function retry()
{
$this->connect($this->$ip,$this->$port); //Line 40, where I'm getting the error now.
$this->join($this->$roomid);
while($this->read()!="DIED");
}
public function connect($ip, $port)
{
if($this->$soc!=null) socket_close($this->$soc);
$soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if(!$this->$soc)$this->port();
if(!socket_connect($this->$soc,$this->$ip,$this->$port))$this->port();
}
public function port()
{
$this->$port++;
if($this->$port>10038) $this->$port=10038;
$this->retry();
}
public function join($roomid)
{
$this->send('<y m="1" />');
$this->read();
$this->send('<j2 q="1" y="'.$this->$packet['y']['i'].'" k="'.$this->$k.'" k3="0" z="12" p="0" c"'.$roomid.'" f="0" u="'.$this->$userid.'" d0="0" n="'.$this->$name.'" a="'.$this->$avatar.'" h="'.$this->$homepage.'" v="0" />');
$this->port();
$this->$roomid;
}
public function send($msg)
{
echo "\n Successfully connected.";
socket_write($this->$soc, $this->$msg."\0", strlen($this->$msg)+1);
}
public function read($parse=true)
{
$res = rtrim(socket_read($this->$soc, 4096));
echo "\nSuccessfully connected.";
if(strpos(strtolower($res), "Failed"))$this->port();
if(!$res) return "DIED";
$this->lastPacket = $res;
if($res{strlen($res)-1}!='>') {$res.=$this->read(false);}
if($parse)$this->parse($res);
return $res;
}
public function parse($packer)
{
$packet=str_replace('+','#più#',str_replace(' ="',' #=#"',$packet));
if(substr_count($packet,'>')>1) $packet = explode('/>',$packet);
foreach((Array)$packet as $p) {
$p = trim($p);
if(strlen($p)<5) return;
$type = trim(strtolower(substr($p,1,strpos($p.' ',' '))));
$p = trim(str_replace("<$type",'',str_replace('/>','',$p)));
parse_str(str_replace('"','',str_replace('" ','&',str_replace('="','=',str_replace('&','__38',$p)))),$this->packet[$type]);
foreach($this->packet[$type] as $k=>$v) {
$this->packet[$type][$k] = str_replace('#più#','+',str_replace('#=#','=',str_replace('__38','&',$v)));
}
}
}
}
$bot = new BotRaid; //This is where I had the error originally
$bot->retry();
?>
Line 40 is below the "Stop Editing" line. Anyone have any suggestions? Or perhaps need me to clear some things up?
You are accessing the properties of the class incorrectly.
The line:
$this->connect($this->$ip,$this->$port);
Should be:
$this->connect($this->ip, $this->port);
Since there was no local variable called $ip, your expression was evaluating to $this-> when trying to access the property since PHP lets you access properties and functions using variables.
For example, this would work:
$ip = 'ip';
$theIp = $this->$ip; // evaluates to $this->ip
// or a function call
$method = 'someFunction';
$value = $this->$method(); // evaluates to $this->someFunction();
You will have to change all the occurrences of $this->$foo with $this->foo since you used that notation throughout the class.
As noted in the comment by #Aatch, see the docs on variable variables for further explanation. But that is what you were running into accidentally.

Categories