У меня есть этот класс
namespace core;
class Entity {
private $type;
public function __construct($type, $source=null){
if($this::isValidType($type)){
$this->type = $type;
}else{
throw new Exception("'".$type."' is not a valid type of entity.");
}
}private static function isValidType($type){
return in_array($type, array(
'Thing',
));
}
}
Тогда я использую этот код:
$thing = new core\Entity('Not a Thing');
И я ожидаю, что это покажет "Not a Thing" is not a valid Type of entity
но вместо этого я получаю
Неустранимая ошибка: класс ‘core \ Exception’ не найден в {root / to / my / file} в строке {line} `.
Я что-то пропустил?
Ты используешь namespace core;
так используя throw new Exception
средства Exception
класс под током namespace
Вместо этого используйте throw new \Exception
Измените это на:
throw new Exception("'".$type."' is not a valid type of entity.");
Это:
throw new \Exception("'".$type."' is not a valid type of entity.");
Других решений пока нет …