简体   繁体   English

如何在Zend Framework中加载一个类

[英]How to load a class in Zend Framework

I got a PHP library ( PHP Markdown ) in my library fold in an standard Zend Framework application. 我在我的library中使用标准的Zend Framework应用程序获得了一个PHP库( PHP Markdown )。 What is the best way to load the file and all it's classes to use in my models and controllers. 加载文件的最佳方法是什么,以及在我的模型和控制器中使用它的所有类。

Structure: 结构体:

library/phpMarkdown/markdown.php

Note: PHP Markdown has a really ugly structure: It's only real "API" is a simple function, not a class. 注意:PHP Markdown有一个非常难看的结构:它只是真正的“API”是一个简单的函数,而不是一个类。 So the elegant was do not work for this exact case , but regarding the question the genearl solution the correctly named files/class is also "the right answer. 因此优雅对于这个确切的情况不起作用,但对于问题,genearl解决方案正确命名的文件/类也是“正确的答案。

Edit So much good input here, really not sure which answer I should accept! 在这里编辑这么多好的输入,真的不确定我应该接受哪个答案! Thanks to you all! 谢谢大家!

The autoloader 自动加载器

Just instantiate the class and the autoloader should find it. 只是实例化类,自动加载器应该找到它。 If it doesn't you need to add the namespace and path. 如果不是,则需要添加命名空间和路径。

If you have a class in the following tree (for exemple) : library/My/Tool.php 如果您在以下树中有一个类(例如):library / My / Tool.php

You will need to add this to your application.ini : 您需要将它添加到您的application.ini:

autoloaderNamespaces[] = "My_"

And then in your code you just call : 然后在您的代码中,您只需调用:

$tool = new My_Tool();

Edit : 编辑:

in the file Tool.php you must follow the Zend Naming Conventions and have something like this : 在文件Tool.php中,您必须遵循Zend命名约定并具有以下内容:

<?php

class My_Tool {

}

For more informations see this : Zend Naming conventions 有关更多信息,请参阅: Zend Naming约定

To keep it simple and just add that one file, you could put something like this in your Bootstrap.php : 为了保持简单,只需添加一个文件,您可以在Bootstrap.php

 protected function _initLoad(){
        Zend_Loader::loadFile('markdown.php', '/../library');

    }

I just copied markdown.php into the application library and put this little function in the bootstrap. 我只是将markdown.php复制到应用程序库中,并将这个小函数放入bootstrap中。 You could also use Zend_Loader::loadClass(); 你也可以使用Zend_Loader :: loadClass(); if you want. 如果你想。

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

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