美文网首页
thinkphp自定义类

thinkphp自定义类

作者: hirolin | 来源:发表于2017-11-28 22:50 被阅读0次

    在thinkphp中
    在Model文件夹下建立SimpleClass.class.php

    在Controller文件下的IndexController中新建方法testSimple()函数.
    在IndexController.class.php文件申明 use Home\Model\SimpleClass 即可在函数中直接实例化

    <?php
    namespace Home\Model;
    
    class SimpleClass
    {
        /**
         * @var string
         */
        public $var = "simple class ";
    
        /**
         *
         */
        public function displayVar(){
            echo $this->var.' hello';
        }
    
    }
    
    <?php
    namespace Home\Controller;
    use Think\Controller;
    use Home\Model\SimpleClass;
    
    class IndexController extends Controller {
    
        /**
         * test
         */
        public function testSimple(){
            $simple = new SimpleClass();
            $simple->displayVar();
        }
    }
    

    若没有使用use, 也可直接实例化对象
    $simple = new \Home\Model\SimpleClass;

    相关文章

      网友评论

          本文标题:thinkphp自定义类

          本文链接:https://www.haomeiwen.com/subject/oijybxtx.html