如何在CakePHP 3.0中使用我自己的外部类? [英] How can I use my own external class in CakePHP 3.0?
问题描述
这是我对Stack Overflow的第一个问题,但我已经在这里找到了数百个好的答案。
无论如何:我在CakePHP 3.0中创建一个应用程序应用程序我想使用我写的php类绘制数据的SVG图。在CakePHP 3项目中使用这个类的正确方法是什么?
更具体地说:
*什么是命名约定?我需要使用特定的命名空间吗?
*我在哪里放置包含PHP类的文件?
最好的问候Jon
命名约定是什么?我需要使用特定的命名空间吗?
您的SVG图类应该有一个命名空间。对于命名空间,您可以查看 http://php.net/manual/en/language.namespaces.rationale.php
在哪里放置包含PHP类的文件?
-
在供应商
中创建作者的文件夹(这里可能是您的姓名) -
然后在里面创建你的类
约定是vendor / $ author / $ package。您可以阅读更多 http://book.cakephp.org/3.0/ en / core-libraries / app.html#loading-vendor-files
a)包括:
require_once(ROOT .DS。'Vendor'。DS。'MyClass'。DS。'MyClass.php');
(以您的文件夹名称取代MyClass,以您的filename.php取代MyClass.php)
b )使用它
:在控制器中使用MyClass \MyClass; $ <$> $ <$>
例如我想在控制器中添加MyClass。适用于我的步骤
- 创建vendor\MyClass文件夹
- 在该文件夹中粘贴MyClass.php
- 在MyClass.php顶部添加
命名空间MyClass;
$ b
MyClass.php有以下代码:
命名空间MyClass;
class MyClass
{
public $ prop1 =我是一个类属性!
public function setProperty($ newval)
{
$ this-> prop1 = $ newval;
}
public function getProperty()
{
return $ this-> prop1。 < br />;
}
}
-
添加
使用控制器顶部的MyClass\MyClass;
-
在我的控制器动作。我的操作示例
public function test()
{
require_once(ROOT。 。DS。MyClass。DS。MyClass.php);
$ obj = new MyClass;
$ obj2 = new MyClass;
echo $ obj-> getProperty();
echo $ obj2-> getProperty();
exit;
}
This is my first question on Stack Overflow, but I have found hundreds of great answers here!
Anyhow: I am creating an application in CakePHP 3.0, in this application I want to draw SVG graphs of data using a php class that I have written. What would be the proper way to go about using this class in my CakePHP 3 project?
More specifically:
*What are the naming conventions? Do I need to use a specific namespace?
*Where do I put the file that contains the PHP class?
*How can I include it and use it in a controller or a view?
Best regards Jon
What are the naming conventions? Do I need to use a specific namespace?
Your SVG graphs class should have a namespaces. For namespaces you can see http://php.net/manual/en/language.namespaces.rationale.php
Where do I put the file that contains the PHP class?
Create a folder by author(here might be your name, as you are the author) in vendor
Then create your class inside of it convention is vendor/$author/$package . You can read more http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
How can I include it and use it in a controller or a view?
a) To include:
require_once(ROOT .DS. 'Vendor' . DS . 'MyClass' . DS . 'MyClass.php');
(replace MyClass by your foldername and MyClass.php by your filename.php)
b) To use it:
add use MyClass\MyClass;
in your controller
For example I want to add MyClass in a controller. Steps that worked for me
- Creating vendor\MyClass folder
- Pasting MyClass.php in that folder
- adding
namespace MyClass;
at the top of MyClass.php
MyClass.php have following code for example:
namespace MyClass;
class MyClass
{
public $prop1 = "I'm a class property!";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
Adding
use MyClass\MyClass;
at the top of controllerThen including it in my controller action. My action sample
public function test() { require_once(ROOT .DS. "Vendor" . DS . "MyClass" . DS . "MyClass.php"); $obj = new MyClass; $obj2 = new MyClass; echo $obj->getProperty(); echo $obj2->getProperty(); exit; }
这篇关于如何在CakePHP 3.0中使用我自己的外部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!