Asked  7 Months ago    Answers:  5   Viewed   38 times

I'm implementing namespaces in my existing project. I found that you can use the keyword 'use' to import classes into your namespace. My question is, can I also import all the classes from 1 namespace into another. Example:

namespace foo

    class bar

        public static $a = 'foobar';



    use foo;  //This doesn't work!
    echo bar::$a;

Update for PHP 7+

A new feature in PHP 7 is grouped declarations. This doesn't make it as easy as using 1 'use statement' for all the classes in a given namespace, but makes it somewhat easier...

Example code:

// Pre PHP 7 code
use somenamespaceClassA;
use somenamespaceClassB;
use somenamespaceClassC as C;

// PHP 7+ code
use somenamespace{ClassA, ClassB, ClassC as C};

See also:



This is not possible in PHP.

All you can do is:

namespace Foo;

use Bar;

$obj = new BarSomeClassFromBar();
Wednesday, March 31, 2021
answered 7 Months ago

The manual specifies the backslash as unnecessary, which naturally means that if you still use it that the meaning is equivalent. However, as you have pointed out, the manual says that it is supposedly not allowed, which is false.

However, there is something else troubling with the manual. They advertise this:

// importing a global class
use ArrayObject;

If it is true that import names are not processed relative to the current namespace, then use ArrayObject and use ArrayObject must have the same meaning. What else could use ArrayObject refer to other than the global one? In practice, the engine will import the global one.

Also, with bugs such as this:

I believe there is confusion over what the standard is supposed to be.

To answer your question: there is no difference. However, if I was the engine developer who was also a believer of the no-leading-slash standard, then I wouldn't need to consider a case where someone wrote use Exception;. I believe this was likely the case.

Wednesday, March 31, 2021
answered 7 Months ago

If you don't keep track of which classes exist, autoloading won't help you.

But the situation would be pretty much the same if you tried to include all files and see which classes would come out of it, when there would be a chance that you missed a file.

Whatever it is what you try to do: Try it differently. I highly doubt you have to instantiate ALL these classes when serving one single request. It would not be very beneficial to the performance if you had to scan ALL declared classes for an interface, and then create an instance for all of them.

Provide some kind of registering method to let the application know about your classes. If you really have to use them all, so be it. But scanning whatever code has been run previously does not sound right (and you didn't explain what you do at all).

Saturday, May 29, 2021
answered 5 Months ago

Looks like a known issue in PhpStorm.

Saturday, May 29, 2021
answered 5 Months ago

It is possible to have one main gulpfile.js from which to call tasks from other gulp files.js using the require-dir module. From the projects README use it like this:

Split tasks across multiple files

If your gulpfile.js is starting to grow too large, you can split the tasks into separate files by using the require-dir module.

Imagine the following file structure:

├── dev.js
├── release.js
└── test.js

Install the require-dir module:

npm install --save-dev require-dir

Add the following lines to your gulpfile.js file.

var requireDir = require('require-dir');
var dir = requireDir('./tasks');
Tuesday, September 14, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :