4

I am trying to use Azure storage for php, the setup steps are to include the namespace, include composer autoload and then use the azure classes, so I have the following. However further down I use the class Microgrid and it's not found because of the namespace, it is in a different directory. How can I use other classes that aren't part of that namespace? Also, what's the correct way to specify your namespace path? It's in a different directory relative to the page this is for and the one I am using is not at the root, should I start at the root?

namespace MicrosoftAzure\Storage;
use \MicrosoftAzure\Storage\Common\ServicesBuilder;
use \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions;
use \MicrosoftAzure\Storage\Blob\Models\PublicAccessType;
use \MicrosoftAzure\Storage\Common\ServiceException;

require_once '/var/www/html/vendor/autoload.php';


$action = MicroGrid::GetParameter('action');
3
  • 2
    use \namespace\where\microgrid\located\MicroGrid; ? Commented Nov 22, 2016 at 17:25
  • if your class doesn't have a namespace, you can try to use this: $action = \MicroGrid::GetParameter('action'); Commented Nov 22, 2016 at 17:33
  • When you declare namespace is it from the root or relative? Commented Nov 22, 2016 at 17:37

3 Answers 3

4

ClassNames are considered relative to the current namespace unless they start with a \

This means that inside the MicrosoftAzure\Storage namespace you can use the relative namespace for class.

If you want to call a class from a different namespace you should call fully-qualified name for it like

$action = \MicrosoftAzure\WhereEver\MicroGrid::GetParameter('action');

or use the name space or unique class with fully-qualified name

use \MicrosoftAzure\WhereEver;

or

use \MicrosoftAzure\WhereEver\MicroGrid;

then:

$action = MicroGrid::GetParameter('action');

Edited to make it clear

namespaces allow us to avoid naming collisions and to alias long names caused by avoiding naming collisions.

it depends to your autoloader for a simple example I create a new project and make this autoloader in the index.php located at root directory

function __autoload($className){
    //if it's a full name with windows style slashes correct the path
    $file_name = str_replace('\\', '/', $className);
    require_once("vender/src/".$file_name.".php");
}

when I call $date = new \App\Utility\Date(); autoloader will require this file:

verdor/src/App/Utility/Date.php

and in Date.php I used this name space namespace App\Utility;

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

1 Comment

Ok, well I don't understand how namespaces should be declared in the first place, my azure library is in ../vendor/MSAzure/Storage and microgrid in ../include/classes, should i be declaring the namespace from the root directory and how could I go back so I can use my microgrid class?
2

PHP does not provides a class autoloader out of the box.

There are many autoloaders for PHP, and the most common autoloader standard is the PSR-4, used by many frameworks and applications.

If you are not using an autoloader, you should require every class file (and recursive dependencies) before using it.

Azure uses Composer Autoloader and PSR-4.

You should use Composer Autoloader on your project, then import your class from the right namespace (you are not importing it on your example code)

5 Comments

I'm using composer, forgot to include that line, what do you mean about importing?
the use Some\Class\Namespace\ClassName is a import statement. You are not importing MicroGrid class anywhere
Also I didn't found any file called MicroGrid on azure php sdk. Are you sure that's the class name?
Microgrid is an unrelated class, it is included using an autoloader. It's also in a different directory, I guess that's part of my problem .. azure library is in ../vendor/MSAzure/Storage and microgrid in ../include/classes, should i be declaring the namespace from the root directory? so it would be like ../html/vendor/MSAzure and I can use ../html/include/microgrid?
Then you should load manually your MicroGrid class using PHP's require('path/to/your/class.php); at the top of your file.
1

A namespace basically groups your classes together. You can use something outside your namespace by explicitly using it e.g.

namespace App;

use Illuminate\Database\Eloquent\Model;

In this case I'm 'grouping' this and other classes in a namespace called 'App' but I want to use 'Model' provided by Eloquent (the Eloquent class having a namespace of 'Illuminate\Database\Eloquent') in this class.

If 'Microgrid' is not part of your current namespace, you'll need to explicitly add its namespace in your 'use' statements.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.