Skip to content

Commit 30ccbca

Browse files
committed
Fix undefined property warning in Item::__call magic method
Add property_exists() checks before accessing dynamic properties in get, has, and add methods to prevent PHP warnings when methods like add_category() are called for properties that don't exist on the class. Fixes #541
1 parent 99fdce3 commit 30ccbca

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

includes/Entity/class-item.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,19 @@ public function __call( $method, $params ) {
115115
$var = strtolower( substr( $method, 4 ) );
116116

117117
if ( strncasecmp( $method, 'get', 3 ) === 0 ) {
118-
return $this->$var;
118+
return property_exists( $this, $var ) ? $this->$var : null;
119119
}
120120

121121
if ( strncasecmp( $method, 'has', 3 ) === 0 ) {
122-
return ! empty( $this->$var );
122+
return property_exists( $this, $var ) && ! empty( $this->$var );
123123
}
124124

125125
if ( strncasecmp( $method, 'set', 3 ) === 0 ) {
126126
$this->$var = current( $params );
127127
}
128128

129129
if ( strncasecmp( $method, 'add', 3 ) === 0 ) {
130-
if ( ! $this->$var ) {
130+
if ( ! property_exists( $this, $var ) || ! $this->$var ) {
131131
call_user_func( array( $this, 'set_' . $var ), current( $params ) );
132132
return true;
133133
}

0 commit comments

Comments
 (0)