Flight PHP跨类调用函数报错Undefined variable: connector 求助
Hey there! The issue here is a simple one—you're trying to call a method on a variable that doesn't exist in the current scope, even though you've already created the instance correctly as a class property.
Let's break down what's happening in your code:
public function __construct() {
$this->connector = new deliveryServiceConnector(DOLIBARR_API_KEY,DOLIBARR_ROOT_URL,null, $this->nodeName);
$connector->testDisplayDev();
}
You've assigned the deliveryServiceConnector instance to $this->connector (a property of your client class), but when you try to call testDisplayDev(), you're using $connector—which PHP treats as a local variable that hasn't been defined anywhere in the constructor method.
The Fix
All you need to do is use the $this-> prefix to reference the class property you just created:
public function __construct() { $this->connector = new deliveryServiceConnector(DOLIBARR_API_KEY,DOLIBARR_ROOT_URL,null, $this->nodeName); $this->connector->testDisplayDev(); // Use $this-> to access the class property }
Quick Explanation
In PHP class methods, any property belonging to the class (like the connector you set up) must be accessed with $this-> followed by the property name. Without this prefix, PHP looks for a local variable named $connector, which doesn't exist in your code—hence the "Undefined variable" error.
内容的提问来源于stack exchange,提问作者Hounaida Rekik

