I have made a class inside a namespace which is working perfectly fine but I think the code can be better. Right now I have to use a '/' for every object inside the class which is not the best thing to do I assume.
namespace Classes{
use Basic
class Enemy extends Basic{
// Constructor
public function __construct(){
parent::__construct("Enemy", new \Defense("Block", ""), 60, [new \Attack("Punch", 50));
}
}
}
Is it possible to do something like this? So I dont have to use a '/' for every object but just for the class itself.
namespace Classes{
use Basic
class Enemy extends \Basic{
// Constructor
public function __construct(){
parent::__construct("Enemy", new Defense("Block", ""), 60, [new Attack("Punch", 50));
}
}
}
I tried to do some research on Google however the only thing I can find is: https://www.w3schools.com/php/php_namespaces.asp and here is not explained how to do such thing.
use Basic;so you don't need a backslash before it. Just do:extends Basic. Do the same with the Defence and Attach classes:use Defense;anduse Attack;and then just doDefenseandAttackin your code. When you add a class usinguse SomeNamespace\ClassName;, PHP imports that class so you can simply doClassNamewithout the backslash. You can read more in the manualnamespace FooBar;without the{and}. If you have multiple namespaces/classes in the same file, I would refactor the code to be one class/namesapce per file.use Defense;to be able to use that class without the backslash. That imports the class so you can use justDefensewithout the backslash. Read through the link I posted since it explains all this very well.