且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用命名空间时找不到 PHP 类

更新时间:2022-10-15 09:57:51

我们可以通过两种方式解决命名空间问题

1)我们可以只使用命名空间并要求

2) 我们可以使用 Composer 并使用自动加载!

第一种方式(命名空间和要求)

Class1.php(定时器类)

命名空间实用程序;类定时器{公共静态函数{}}

Class2.php(验证类)

命名空间实用程序;需要Class1.php";//一些有趣的点要记下来!//我们没有使用关键字use"//我们需要使用相同的命名空间,即Utility"//因此,Class1.php 和 Class2.php 都有namespace Utility"//require通常是文件路径!//我们在require"中没有提到类名;//如果 Class1.php 文件在另一个文件夹中怎么办?//例如:src/utility/Stopwatch/Class1.php"//然后要求将是秒表/Class1.php"//您的命名空间仍然是命名空间实用程序";对于 Class1.php班级验证{定时器::某些功能();}

第二种方式(使用 Composer 和自动加载方式)

制作 composer.json 文件.根据您的示例src/Utility"我们需要在 src 文件夹之前创建一个 composer.json 文件.示例:在名为 myApp 的文件夹中,您将拥有 composer.json 文件和 src 文件夹.

 {自动加载":{psr-4":{"Utility\":"src/utility/"}}}

现在转到该文件夹​​,在 composer.json 文件所在的文件夹位置打开您的终端.现在在终端输入!

 composer dump-autoload

这将创建一个供应商文件夹.因此,如果您有一个名为MyApp"的文件夹您将看到 vendor 文件夹、src 文件夹和 composer.json 文件

Timer.php(定时器类)

命名空间实用程序;类定时器{公共静态函数 somefunction(){}}

Verification.php(验证类)

命名空间实用程序;需要../../vendor/autoload.php";使用工具定时器;班级验证{定时器::某些功能();}

当你有一个复杂的文件夹结构时,这个方法更强大!!

I am new with this namespace thing.

I have 2 classes(separate files) in my base directory, say class1.php and class2.php inside a directory src/.

class1.php

namespace srcutilityTimer;

class Timer{
    public static function somefunction(){

    }
}

class2.php

namespace srcutilityVerification;
use Timer;

class Verification{
     Timer::somefunction();
}

When I execute class2.php, i get the Fatal error that

PHP Fatal error: Class 'Timer' not found in path/to/class2.php at line ***

I read somewhere on SO, that I need to create Autoloaders for this. If so, how do I approach into creating one, and if not, then what else is the issue?

UPDATE

I created an Autoloader which will require all the required files on top of my php script. So, now the class2.php would end up like this.

namespace srcutilityVerification;
require '/path/to/class1.php'
use Timer;
//or use srcutilityTimer ... both doesn't work.

class Verification{
     Timer::somefunction();
}

This also does not work, and it shows that class not found. But, if I remove all the namespaces, and use's. Everything works fine.

We can solve the namespace problem in two ways

1) We can just use namespace and require

2) We can use Composer and work with the autoloading!

The First Way (Namespace and require) way

Class1.php (Timer Class)

namespace Utility;

class Timer
   {
    public static function {}
   }

Class2.php (Verification Class)

namespace Utility;
require "Class1.php";

//Some interesting points to note down!
//We are not using the keyword "use" 
//We need to use the same namespace which is "Utility" 
//Therefore, both Class1.php and Class2.php has "namespace Utility"

//Require is usually the file path!
//We do not mention the class name in the require " ";
//What if the Class1.php file is in another folder?
//Ex:"src/utility/Stopwatch/Class1.php"  

//Then the require will be "Stopwatch/Class1.php"
//Your namespace would be still "namespace Utility;" for Class1.php

class Verification
   {
     Timer::somefunction();
   }

The Second Way (Using Composer and the autoloading way)

Make composer.json file. According to your example "src/Utility" We need to create a composer.json file before the src folder. Example: In a folder called myApp you will have composer.json file and a src folder.

   {
     "autoload": {
     "psr-4": {
        "Utility\":"src/utility/"
        }
     }   
   }

Now go to that folder open your terminal in the folder location where there is composer.json file. Now type in the terminal!

   composer dump-autoload

This will create a vendor folder. Therefore if you have a folder named "MyApp" you will see vendor folder, src folder and a composer.json file

Timer.php(Timer Class)

namespace Utility;

class Timer
     {
      public static function somefunction(){}
     }

Verification.php (Verification Class)

namespace Utility; 
require "../../vendor/autoload.php"; 
use UtilityTimer; 

class Verification
  {
     Timer::somefunction(); 
  }

This method is more powerful when you have a complex folder structure!!