简体   繁体   中英

Don't understand php namespace, how can I call function from another class?

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');

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;

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM