CodeIgniter; $ this在非对象上下文中的用法 [英] CodeIgniter; $this usage in non object context

查看:164
本文介绍了CodeIgniter; $ this在非对象上下文中的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的CodeIgniter项目中编辑我的 config / database.php ,所以我不必每次推新的时候不断更改数据库信息版本到服务器。为了做后者,我创建了一个 $ debug 变量,如下所示,它检查uri以查看该网站是否在我的本地主机上运行项目上传之前)或在实际服务器上。

I am trying to edit my config/database.php within my CodeIgniter project so that I don't have to keep changing the database information every time I push a new version to the server. To do the latter, I have created a $debug variable, shown below, which checks against the uri to see if the site is being run on my localhost machine (where I build the project before upload) or on the actual server.

不幸的是,在database.php文件中,我没有访问 $ this 变量,我个人是PHP的新OOP,我不完全确定一个方法。

Unfortunately in the database.php file, I don't have access to the $this variable, and as I am personally new to OOP in PHP, I am not entirely sure of a way around this. Please can you tell me how I can do so?

感谢,

最多。

致命错误:在第51行应用程序/ config / database.php中不在对象上下文中时使用$ this

Fatal error: Using $this when not in object context in application/config/database.php on line 51

$debug = strpos($this->uri->config->item('base_url'), 'localhost'); //line 51

$db['default']['hostname'] = $debug == TRUE ? 'x' : 'y';
$db['default']['username'] = $debug == TRUE ? 'x' : 'y';
$db['default']['password'] = $debug == TRUE ? 'x' : 'y';
$db['default']['database'] = $debug == TRUE ? 'x' : 'y';


推荐答案

您需要获取codeigniter对象的实例 -

You need to a get an instance of the codeigniter object -

$CI = get_instance();

对于旧版本的php,请使用=&

For older versions of php, use =&

然后,使用 $ CI ,无论您通常使用 $ this

Then, use $CI wherever you would normally use $this

这篇关于CodeIgniter; $ this在非对象上下文中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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