Access to an undefined property object::$primary_key
159 if (!isset($f->table) || $f->table == '' || $R == '') {
160 $A = [];
161 } else {
162 $s = "SELECT * FROM `$f->table` WHERE `$f->primary_key` = ?"; 163 $t = nuRunQuery($s, [$R]);
164 $A = db_fetch_array($t);
165 }
Access to an undefined property object::$sob_all_height
1587 if ($r->sob_all_type == 'textarea') {
1588 $oh = $r->sob_all_height + 16;
1589 } elseif (isset($r->sob_select_2) && $r->sob_select_2 == '1') {
1590 $oh = $r->sob_all_height + 13;1591 } else {
1592 $oh = $r->sob_all_height;
1593 }
Description
The property you are trying to access is not defined and will cause unexpected behavior when used.
This issue will be raised in the following cases:
- An undefined property is accessed.
- A private property from a parent class is accessed.
- A protected property of an instance of a class is accessed.
- The variable whose property is being accessed is not an instance of a class, or is not defined.
- Using properties via
__get()
or__set()
magic methods.
Bad practice
class Admin
{
public function getName(): string
{
return $this->name; // $name is not declared as a property of the class Admin
}
}
class User
{
private string $name = 'John Doe';
}
class Admin extends User
{
public function getName(): string
{
return $this->name; // $name is a private property of the parent class
}
}
class User
{
protected string $name = 'John Doe';
}
class Admin
{
public function getName(): string
{
$user = new User;
return $user->name; // $name is a protected property defined in the User class, and cannot be accessed outside of it or its descendent classes
}
}
class Admin
{
public string $user = 'John';
public function getName(): string
{
return $this->user->name; // $this->user is not an instance of any class
}
}
Accesing magic properties:
Though in this example, the __set()
method is implemented, it doesn't guarantee that properties(username
in this case) being accessed will always be there!
class Admin
{
public function __set(string $name, mixed $value): void
{
$this->{$name} = $value;
}
public function getUsername(): string
{
// invalid: property "username" doesn't exist
return $this->username;
}
}
$admin = new Admin();
// Pay attention to this line. If this line weren't present, the "getUsername" method wouldn't be able to find the "username" property.
$admin->username = 'john';
$admin->getUsername();
Recommended
class Admin
{
public string $name = 'John Doe';
public function getName(): string
{
return $this->name;
}
}
class User
{
protected string $name = 'John Doe';
}
class Admin extends User
{
public function getName(): string
{
return $this->name;
}
}
class User
{
public string $name = 'John Doe';
}
class Admin
{
public function getName(): string
{
$user = new User;
return $user->name;
}
}
class User
{
public string $name = 'John Doe';
}
class Admin
{
public ?User $user;
public function __construct()
{
$this->user = new User;
}
public function getName(): string
{
return $this->user->name;
}
}
Accesing magic properties:
Use @property
PHPDoc tag at the class level to let analyzer know what properties it can access. It'll fix the issue causing this error.
/**
* @property string $username
*/
class Admin
{
public function __set(string $name, mixed $value): void
{
$this->{$name} = $value;
}
public function getUsername(): string
{
return $this->username;
}
}