如何在CakePHP 3.0中使用我自己的外部类? [英] How can I use my own external class in CakePHP 3.0?

查看:276
本文介绍了如何在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类的文件?


  1. 在供应商

    中创建作者的文件夹(这里可能是您的姓名)

  2. 然后在里面创建你的类
    约定是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。适用于我的步骤





  1. 创建vendor\MyClass文件夹

  2. 在该文件夹中粘贴MyClass.php

  3. 在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 />;
}
}




  1. 添加使用控制器顶部的MyClass\MyClass;


  2. 在我的控制器动作。我的操作示例

      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?

  1. Create a folder by author(here might be your name, as you are the author) in vendor

  2. 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

  1. Creating vendor\MyClass folder
  2. Pasting MyClass.php in that folder
  3. 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 />";
    }
}

  1. Adding use MyClass\MyClass; at the top of controller

  2. Then 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆