Using namespace in if / else statements

前端 未结 4 608
一个人的身影
一个人的身影 2020-12-09 12:59

I am manipulating the same file to manage two external api classes.

One api class is based on namespaces, the other one is not.

What I would like to do is s

相关标签:
4条回答
  • 2020-12-09 13:19

    Please see Scoping rules for importing

    The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

    All use does is import a symbol name into the current namespace. I would just omit the import and use the fully qualified class name, eg

    switch ($api) {
        case 'foo' :
            require_once('foo.php');
            $someVar = new SomeClass();
            break;
        case 'bar' :
           require_once('bar.php');
           $someVar = new \xxxx\TheClass();
           break;
       default :
           throw new UnexpectedValueException($api);
    }
    

    You can also simply add the use statement to the top of your script. Adding it does not commit you to including any files and it does not require the symbol to be known, eg

    use xxxx\TheClass;
    
    switch ($api) {
        case 'foo' :
            require_once('foo.php');
            $someVar = new SomeClass();
            break;
        case 'bar' :
           require_once('bar.php');
           $someVar = new TheClass(); // imported above
           break;
       default :
           throw new UnexpectedValueException($api);
    }
    
    0 讨论(0)
  • 2020-12-09 13:22

    Another option for API class versioning is to set the classname as a variable conditionally.

    // Get class name
    switch ($api_version) {
      case 'v0':
        // No class namespace
        $class = 'Handler';
        break;
      case 'v1':
        // Namespaced class
        $class = 'API\App\v1\Handler';
        break;
      case 'v2':
        // Namespaced class
        $class = 'API\App\v2\Handler';
       break;
     default:
       throw new Exception('Unknown API version: ' . $api_version);
    }
    // Get class object
    $handler = new $class();
    
    0 讨论(0)
  • 2020-12-09 13:25

    Alright, just confirming what i said was true.. Try something like this :

    *Test_1.php*

    $API = "test_1";
    if ($API === "test"){
    
    }elseif ($API === "test_1"){
        require ("test.php");
        $API = new daryls\testt;
    }
    
    $API->test();
    

    test.php

    namespace daryls;
    class  testt {
        public function test(){
            echo "Started"; 
        }
    }
    

    Running this has worked without a hitch

    0 讨论(0)
  • 2020-12-09 13:32

    Use statements should be placed before any executable code (you can have namespaces, classes, functions and constants definitions). Actually it can, just have to be placed unconditionally in some namespace, so no ifs or inside functions. Also don't be afraid of putting use at the top, it does not load any class or instantiate object. it acts only as alias that is used when encountered later during execution.

    As for having them in one file, it is possible to have many namespaces and even global namespace in one file:

    <?php   
    namespace
    {
        class myclass{}
    }
    namespace mynamespace 
    {
        class myclass{}
    }
    

    But I strongly discourage such "management" of code. Each class should have it's own file.

    0 讨论(0)
提交回复
热议问题