hi i have this simple class for example:
class Asd{
private $_url = $this->config->config['url'];
}
but it doesn't get the config param any clue on how to do that?
How do i call a config param in private var ?
You can do like this :
class Asd {
private $_url;
public function __construct() {
$CI =& get_instance();
$this->_url = $CI->config->config['url'];
}
}
You can't declare class property as you want.
Variables can be assigned only constructor!
class Asd{
private $_url;
public function __construct() {
$CI =& get_instance();
$this->_url = $CI->config->config['url'];
}
}
Below is not possible
class Asd{
private $_url='www.so.com';
public function __construct() {
$CI =& get_instance();
$this->_url = $CI->config->config['url'];
}
}
Related
I have file init.php:
<?php
require_once 'config.php';
init::load();
?>
with config.php:
<?php
$config = array('db'=>'abc','host'=>'xxx.xxx.xxx.xxxx',);
?>
A class with name something.php:
<?php
class something{
public function __contruct(){}
public function doIt(){
global $config;
var_dump($config); // NULL
}
}
?>
Why is it null?
In php.net, they told me that I can access but in reality is not .
I tried but have no idea.
I am using php 5.5.9.
The variable $config in config.php is not global.
To make it a global variable, which i do NOT suggest you have to write the magic word global in front of it.
I would suggest you to read superglobal variables.
And a little bit of variable scopes.
What I would suggest is to make a class which handles you this.
That should look something like
class Config
{
static $config = array ('something' => 1);
static function get($name, $default = null)
{
if (isset (self::$config[$name])) {
return self::$config[$name];
} else {
return $default;
}
}
}
Config::get('something'); // returns 1;
Use Singleton Pattern like this
<?php
class Configs {
protected static $_instance;
private $configs =[];
private function __construct() {
}
public static function getInstance() {
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
private function __clone() {
}
private function __wakeup() {
}
public function setConfigs($configs){
$this->configs = $configs;
}
public function getConfigs(){
return $this->configs;
}
}
Configs::getInstance()->setConfigs(['db'=>'abc','host'=>'xxx.xxx.xxx.xxxx']);
class Something{
public function __contruct(){}
public function doIt(){
return Configs::getInstance()->getConfigs();
}
}
var_dump((new Something)->doIt());
Include the file like this:
include("config.php");
class something{ ..
and print the array as var_dump($config); no need of global.
Change your class a bit to pass a variable on the constructor.
<?php
class something{
private $config;
public function __contruct($config){
$this->config = $config;
}
public function doIt(){
var_dump($this->config); // NULL
}
}
?>
Then, if you
include config.php
include yourClassFile.php
and do,
<?php
$my_class = new something($config);
$my_class->doIt();
?>
It should work.
Note: It is always good not to use Globals (in a place where we could avoid them)
I use hook mechanism in Codeigniter. The kind of hook is post_controller_constructor.
There is one private object inside class hook`s:
private $settings = array();
This object is filled after executing hook.
How I can get access to $settings from libraries CI and controllers?
Class:
<? class LCode_module
{
private $CI;
private $_default_lang = "en";
private $_sufixLangDefault = "_EN";
private $allowedLanguages = array();
private $_countryCurrent;
private $countries = array();
private $languages = array();
private $settings = array();
public function __construct()
{
$this->CI =& get_instance();
/* Load lists */
$this->CI->load->library('listdata');
$this->countries['country'] = $this->CI->listdata->country;
$this->countries['country_code'] = $this->CI->listdata->country_code;
$this->countries['country_lang'] = $this->CI->listdata->country_lang;
$this->languages = $this->CI->listdata->languages_sys;
}
public function route()
{
//Here I put data to $settings
}
}
Method route is init method in hook
At the end of constructor:
/* Add object of class to GI instance */
$this->CI->LCode_module = new stdClass;
$this->CI->LCode_module->settings = &$this->settings;
After I try to get data in controller:
$CI =& get_instance();
$c = $CI->LCode_module;
var_dump($c); // NULL
Use this as your class, I created a static option which you can use whenever you need:
<?php
class LCode_module
{
private $CI;
private $_default_lang = "en";
private $_sufixLangDefault = "_EN";
private $allowedLanguages = array();
private $_countryCurrent;
private $countries = array();
private $languages = array();
private $settings = array();
private static $instance;
private static $static_settings;
public function __construct()
{
$this->CI =& get_instance();
/* Load lists */
$this->CI->load->library('listdata');
$this->countries['country'] = $this->CI->listdata->country;
$this->countries['country_code'] = $this->CI->listdata->country_code;
$this->countries['country_lang'] = $this->CI->listdata->country_lang;
$this->languages = $this->CI->listdata->languages_sys;
self::$instance = &$this;
self::$static_settings = &$this->settings;
}
public function route()
{
//Here I put data to $settings
}
public static function getInstance(){
if (is_null(self::$instance)) { self::$instance = new self(); }
return self::$instance;
}
public static function settings($key = NULL){
$instance = self::getInstance();
if(is_null($key)) return $instance::$static_settings;
return (array_key_exists($key, $instance::$static_settings) ? $instance::$static_settings[$key] : null);
}
}
Then you just call
LCode_module::settings()
when you need to retrieve the settings
This does imply parallel singleton use, which isn't exactly best practice, but it should do the trick for now since hooks only get loaded once. I'm sure CI has a way of performing this, but I'm drawing a blank on it right now.
I have a class in hook folder like this
class CheckAuth {
protected $CI;
public function __construct()
{
$this->CI = get_instance();
}
public function check()
{
$router =& load_class('Router', 'core');
// $controller = $this->CI->router->class;
$controller = $router->fetch_class();
$method = $router->fetch_method();
if($controller!='auth')
{
echo $this->CI->userdata('admin_id');
}
}
}
I show error when I get a session
Fatal error: Call to a member function userdata() on a non-object $this->CI return null.
Try this it will work.
public function __construct()
{
}
public function check()
{
$this->CI = get_instance();
$router =& load_class('Router', 'core');
// $controller = $this->CI->router->class;
$controller = $router->fetch_class();
$method = $router->fetch_method();
if($controller!='auth')
{
echo $this->CI->userdata('admin_id');
}
}
That's because CI isn't loaded yet in some hooks points (pre_controller, pre_system). You are probably trying to load class in some of these.
Edit: Original example removed as it was complex.
The codes provided below doesn't work. I am trying to access the methods defined in a class which is declared in the parent class.
Here is a sample code. Its not working and I'd like to know why
<?php
function & get_instance()
{
return Main::get_instance();
}
class Db{
function select($var)
{
echo $var;
}
}
class Main
{
public $db ;
public $process ;
private static $instance;
function __construct()
{
self::$instance = &$this;
$this->db = new Db ;
$this->process = Process;
}
public static function & get_instance()
{
return self::$instance;
}
}
class Process{
private $main ;
function __construct()
{
$this->main = get_instance() ;
}
function processPayment()
{
$this->main->db->select("hello");
}
}
$main = new Main ;
$main->process->processPayment();
To access members of a parent class, you will have to declare those members protected or public.
For example:
public var $db;
protected var $orders;
I am trying to use the singleton method to access a global object (in this example its "username"). My question is how can I modify this so that in the DB->connect() function I could do echo $this->username; without declaring $username or changing the last 2 lines?
class CI_Base {
private static $instance;
public function CI_Base()
{
self::$instance =& $this;
}
public static function &get_instance()
{
return self::$instance;
}
}
function &get_instance() {
return CI_Base::get_instance();
}
class Foo {
function run() {
$CI = & get_instance();
$CI->username = "test";
$db = new DB;
$db->connect();
}
}
class DB extends Foo {
function connect() {
$CI = & get_instance();
echo $CI->username;
}
}
$foo = new Foo;
$foo->run();
This should work
class Foo {
function __get($field) {
if ($field == "username") {
//don't need to create get_instance function
$CI = CI_Base::get_instance();
return $CI->username;
}
}
}
you can pass all access to non existing fields from Foo to $instance object:
class Foo {
function __get($field) {
$CI = CI_Base::get_instance();
return $CI->$field;
}
}
class DB extends Foo {
function connect() {
// this->username will call __get magic function from base class
echo this->username;
}
}
in php5 you don't need to put ampersand before get_instance becouse all objects are passed by reference.