0

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.

5
  • You have a 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; and use Attack; and then just do Defense and Attack in your code. When you add a class using use SomeNamespace\ClassName;, PHP imports that class so you can simply do ClassName without the backslash. You can read more in the manual Commented May 20, 2022 at 7:45
  • Btw, if you only have one class/namespace in a file, you can simply do: namespace 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. Commented May 20, 2022 at 7:49
  • I used the backlash before it because if I try to do it without the backlash I get the error: Undefined type 'Classes\Defense'. The only way I got this working is with the backlash included, Commented May 20, 2022 at 7:57
  • As I mentioned in my previous comment, you need to add use Defense; to be able to use that class without the backslash. That imports the class so you can use just Defense without the backslash. Read through the link I posted since it explains all this very well. Commented May 20, 2022 at 7:58
  • OH my bad. I did misread it and thought you meant to add the use Defense inside the class object. Thanks! Commented May 20, 2022 at 8:00

1 Answer 1

1

My prefered way is this:

<?php

namespace Classes;

use Basic;
use Defense;
use Attack;

class Enemy extends Basic
{
    const WHAT_IS_THIS_NUMBER = 60;
    const ALSO_THIS_NUMBER = 50;

    public function __construct()
    {
        parent::__construct(
            "Enemy",
            new Defense("Block", ""),
            self::WHAT_IS_THIS_NUMBER,
            new Attack("Punch", self::ALSO_THIS_NUMBER)
        );
    }
}

As usual, w3school had provided ugly and oldscholl code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.