I am trying to get this litte demo to run, but I keep getting a plain string
"getConfiguration:Name (self::getName())is not supported"
(when using self::getName())
or an error-messege:
PHP Notice: Undefined property: Demo::$getName
(when using $this->getName())
This is my Code:
class Demo {
protected $name = "demo";
public function __construct() {
try {
if(true) {
throw new Exception("Name (self::getName())" .
"is not supported");
}
} catch(Exception $e){
echo $e->getMessage(); exit;
}
}
public function getName() {
return $this->demo;
}
}
Now is this simply not possible or am I doing something wrong here?!
EDIT:
Before this I got this working with $this->name
, but I would rather use a function if it is possible and not somehow a very bad idea.
You are calling a function statically that is not static. Referencing to $this might also be problematic inside a construcotr, especially if it failed.
You should also change your Exception to not contain the call inside a string.
throw new Exception("Name (".self::getName().") is not supported");
Change your method to static access. You will also have to make your variable $demo static:
protected static $name;
public static function getName() {
return self::$name;
}
Throwing an exception just to get an echo doesnt make that much sense. You should either just echo your error and exit or throw an exception:
public function __construct() {
throw new Exception("Name (".self::getName().") is not supported");
}
OR
public function __construct() {
echo "Name (".self::getName().") is not supported";
exit;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.