php框架CI(codeigniter)自动加载与自主创建对象操作实例分析

2020-06-06 11:00:15于丽
  public static function getInstance()
  {
    return new TestNewInstance();
  }

使用时直接通过调用此静态函数获得:

$newInstance1 = TestNewInstance::getInstance();
$newInstance1->setAge(20);
$name1 = $newInstance1->getName();

$newInstance2 = TestNewInstance::getInstance();
$newInstance2->setAge(25);
$name2 = $newInstance2->getName();

方法二:直接调用使用new生成对象(此方法就不需要那个静态函数了):

$newInstance1 = new TestNewInstance();
$newInstance1->setAge(30);
$name1 = $newInstance1->getName();
$newInstance2 = new TestNewInstance();
$newInstance2->setAge(38);
$name2 = $newInstance2->getName();

注意,不管生成实例的方法一或者方法二,引入的时候上面的几种引入法都是通用的。

之前的文章提到使用cli调用法,这种静态函数返回实例的方法行不通,我想可能还是引入的时候出问题,以后碰到了再研究。

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

相关文章 大家在看