简体   繁体   English

PSR-0在类加载上的实现

[英]PSR-0 Implementation on Class Loading

I'm not new to PHP but I'm new to PSR. 我不是PHP的新手,但我是PSR的新手。 I've read some of it and I want to give a try to follow this standards on coding but I'm a bit confused how can implement it. 我已经阅读了其中的一些内容,并且想尝试遵循此编码标准,但是我有点困惑如何实现它。 So I need a simple advice from you guys on how to implement it based on the example I will provide. 因此,根据我将提供的示例,我需要您提供有关如何实现它的简单建议。

Directory Structure 目录结构

 /models
    User.php
 /controller
    controller.php
 /view
    index.php

Model.php Model.php

Class User
{
  public function foo()
  {
    // Do something
  }
}

How can I call that class on my controller.php in PSR-0 approach? 如何以PSR-0方法在controller.php上调用该类? I've read something like 我读过类似的东西

namespace, use

and this 和这个

   function autoload($className)
   {
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName;
   }

But i dunno how and where to put those codes. 但是我不知道如何以及在哪里放置这些代码。

Thanks! 谢谢!

It means you have a folder for every namespace you are using. 这意味着您为所使用的每个命名空间都有一个文件夹。

So if you define a class in a namespace say: 因此,如果您在名称空间中定义一个类,请说:

<?php
namespace Nicemodels;

class Niceuser { ... }

Then the file Niceuser.php needs to be in .../models/Nicemodels/ 然后,文件Niceuser.php需要位于... / models / Nicemodels /

You still need to make sure you handle the models directory correctly. 您仍然需要确保正确处理models目录。 You could start one on a higher level and put all your models in the Models namespace (recommended). 您可以在更高级别上启动一个模型,然后将所有模型放入“模型”名称空间中(推荐)。

So above example becomes: 因此,上面的示例变为:

<?php
namespace Models\Nicemodels;

class Niceuser { ... }

The use statement is used to import classes from another namespace: use语句用于从另一个名称空间导入类:

<?php
namespace Models\Nicemodels;

use Models\Normaluser

class Niceuser extends Normaluser { ... }

You autoloader reflects this namespace to directory behaviour in the line 您的自动加载器将此名称空间反映到该行中的目录行为

$fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;

Here you convert namespace separators '\\' into directory separators. 在这里,您将名称空间分隔符“ \\”转换为目录分隔符。

You need to tell your autoload the starting point of all this. 您需要告诉自动加载所有这些内容的起点。 So you if you don't use the Models namespace you need to point to your models/ folder as start. 因此,如果您不使用Models名称空间,则需要将其作为开始指向模型/文件夹。 And you need to make sure of the case you start your filenames with. 并且您需要确保以这种方式开始文件名。 Otherwise the autoloader will not find your classes. 否则,自动加载器将找不到您的类。


If you want to use such a class in your controllers you do: 如果要在控制器中使用此类,请执行以下操作:

$user = new \Models\Nicemodels\Niceuser();

You can shorten that, if you import the class: 如果导入该类,则可以缩短该时间:

use Models\Nicemodels\Niceuser;
...
$user = new Niceuser();

You need to register your autoloader with spl_autoload_register() function. 您需要使用spl_autoload_register()函数注册自动加载器。

You've got some nice examples in docs: http://www.php.net/manual/en/function.spl-autoload-register.php 您在文档中有一些不错的示例: http : //www.php.net/manual/en/function.spl-autoload-register.php

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

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