简体   繁体   English

不懂php命名空间,怎样才能从另一个类调用函数?

[英]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. 我试图使用Azure存储为PHP,设置步骤是包括命名空间,包括composer autoload然后使用azure类,所以我有以下。 However further down I use the class Microgrid and it's not found because of the namespace, it is in a different directory. 然而,我进一步向下使用类Microgrid并且由于命名空间而找不到它,它位于不同的目录中。 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 \\ ClassNames被认为是相对于当前命名空间的,除非它们以一个\\开头

This means that inside the MicrosoftAzure\\Storage namespace you can use the relative namespace for class. 这意味着在MicrosoftAzure\\Storage命名空间内,您可以使用类的相对命名空间。

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 这取决于您的自动加载器的一个简单示例我创建一个新项目并在位于根目录的index.php中创建此自动加载器

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(); 当我调用$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; Date.php我使用了这个名称空间命名空间App\\Utility;

PHP does not provides a class autoloader out of the box. PHP 没有提供开箱即用的类自动加载器。

There are many autoloaders for PHP, and the most common autoloader standard is the PSR-4 , used by many frameworks and applications. PHP有许多自动加载器,最常见的自动加载器标准是PSR-4 ,许多框架和应用程序都使用它。

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. Azure使用Composer Autoloader和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) 您应该在项目中使用Composer Autoloader,然后从正确的命名空间导入您的类(您不是在示例代码中导入它)

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. 在这种情况下,我将这个和其他类分组在一个名为'App'的命名空间中,但是我想在这个类中使用Eloquent提供的'Model'(Eloquent类具有'Illuminate \\ Database \\ Eloquent'命名空间') 。

If 'Microgrid' is not part of your current namespace, you'll need to explicitly add its namespace in your 'use' statements. 如果'Microgrid'不是当前命名空间的一部分,则需要在'use'语句中显式添加其命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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