关于构造方法,我基础很差。代码上有许多错误,我写了一个概念。请老师解惑

原创 2019-02-13 15:38:37 279
摘要:<?phpnamespace app\index\common;use think\Controller;class Ceshi extends Controller{ //构造私有化 private function __construct(){} //克隆私有化 private function __clone(){} //创建内部静态属性,保存唯一实例 protected stati

<?php


namespace app\index\common;

use think\Controller;

class Ceshi extends Controller

{

//构造私有化

private function __construct(){}

//克隆私有化

private function __clone(){}

//创建内部静态属性,保存唯一实例

protected static $instance;


//创建外部接口

public static function getInstance()

{

if(!(self::$instance instanceof self))

  {

  self::$instance=new getInstance();

  }

        return self::$instance;

}

public function lianjie(){

$db=[

// 数据库类型

    'type'            => 'mysql',

    // 服务器地址

    'hostname'        => '127.0.0.1',

    // 数据库名

    'database'        => '',

    // 用户名

    'username'        => 'root',

    // 密码

    'password'        => '',

];

}

调用:

<?php

namespace app\index\controller;

use app\index\common\Ceshi as CCceshi;


class Ceshi extends CCceshi

{

   public function index()

   {

    $ceshi=CCceshi::getInstance();

     $lianjie=$ceshi->lianjie();

   } 



批改老师:韦小宝批改时间:2019-02-13 15:52:03
老师总结:写的也挺不错了 基础差不是问题 要记得多练习哦!多练习写代码的时候再多点细心就可以了!

发布手记

热门词条