Codeigniter 3 with get_instance(); in hook file - php

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.

Related

Can not load url helper in codeigniter

I tried to load base_url() in controller, but codeigniter does not load the helper('url'). I also call helper from autoload and the constructor both in the hook, but it's still not working and showing an error "Trying to get property of non-object".
Any idea how can I redirect?
My code:
if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
class Auth_hook {
protected $CI;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->helper('url');
}
public function index(){
redirect(base_url('auth/login'));
print_r("hello!!");
if(isset($_SESSION['name']) == 'TRUE'){
redirect(base_url('auth/admin'));
}
else {
redirect(base_url('auth/login'));
}
}
}
How about this:
class Auth_hook {
protected $CI;
public function __construct() {
$this->CI =& get_instance();
}
public function index(){
// can communicate back with CI by using $this->CI
$this->CI->load->helper('url');
redirect(base_url('auth/login'));
print_r("hello!!");
if(isset($_SESSION['name']) == 'TRUE'){
redirect(base_url('auth/admin'));
}
else {
redirect(base_url('auth/login'));
}
}
}

why is php class not being loaded

Im testing this thing where i'm trying to load a class and use it like this:
$this->model->model_name->model_method();
This is what I've got:
<?php
error_reporting(E_ALL);
class Loader {
public function model($model)
{
require_once("models/" . $model . ".php");
return $this->model->$model = new $model;
}
}
class A {
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->load->model('Test');
$this->text = $this->model->Test->test_model();
}
public function get_text()
{
return $this->text;
}
}
$text = new A();
echo $text->get_text();
?>
Im getting a bunch of errors here:
Warning: Creating default object from empty value in
C:\xampp\htdocs\fw\A.class.php on line 9
Notice: Trying to get property of non-object in
C:\xampp\htdocs\fw\A.class.php on line 24
Fatal error: Call to a member function test_model() on a non-object in
C:\xampp\htdocs\fw\A.class.php on line 24
What am I doing wrong? Thanks for any tip!
P.S. not much in the loaded file:
<?php
class Test {
public function test_model()
{
return 'testmodel';
}
}
?>
In the A class' constructor you are not assigning the "loaded" model to anything and later you are trying to use the $model property which has nothing assigned to it.
Try this:
class A {
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->model = $this->load->model('Test');
$this->text = $this->model->test_model();
}
(...)
Problem may be that you have not defined Loader.model as object but treating it like it is.
class Loader {
public $model = new stdClass();
public function model($model)
{
require_once("models/" . $model . ".php");
return $this->model->$model = new $model();
}
}
When you have your class like this you can use
$this->model->model_name->model_method();
Try the following code(UPDATED) if you want to avoid $this->model = $this->load->model('Test') in the constructor.
You can simply load the models by calling $this->loadModel(MODEL) function
<?php
error_reporting(E_ALL);
class Loader {
private $models = null;
public function model($model)
{
require_once("models/" . $model . ".php");
if(is_null($this->models)){
$this->models = new stdClass();
}
$this->models->$model = new $model();
return $this->models;
}
}
class A{
public $load;
public $model;
public $text;
public function __construct()
{
$this->load = new Loader();
$this->loadModel('Test');
$this->loadModel('Test2');
$this->text = $this->model->Test2->test_model();
}
public function get_text()
{
return $this->text;
}
private function loadModel($class){
$this->model = $this->load->model($class);
}
}
$text = new A();
echo $text->get_text();
?>

Codeigniter / PHP - declare class private var

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'];
}
}

Using $this when not in object context. wordpress plugin

I am building a wordpress plugin in some MVC style and i have setup almost every thing but when using call_user_func to call the request action class, I am not able to use the $this with the requested class.
Here is my code so far...
class Action{
protected $class;
protected $method;
protected $args = array();
protected $template='settings/index';
public function __construct() {
$this->getRequest();
}
public function getRequest(){
if(isset($_GET['c'])){
$route = $_GET['c'];
$parts = explode('/',$route);
if(isset($parts[0]))
$this->class = $parts[0];
if(isset($parts[1]))
$this->method = $parts[1];
if(isset($parts[2]))
$this->args[] = $parts[2];
if(isset($parts[3]))
$this->args[] = $parts[3];
}
}
public function render(){
extract($this->data);
ob_start();
require(LINKWAG_VIEW .DS. $this->template . P);
$this->output = ob_get_contents();
ob_end_clean();
return $this;
}
public function output(){
echo $this->output;
}
}
class Grv extends Action{
public function __construct() {
parent::__construct();
add_action('admin_menu', array($this,'setup'));
}
public function setup(){
add_menu_page( 'LinkWag', 'LinkWag', 'manage_options', 'linkwag',array($this,'execute'), plugins_url( 'myplugin/images/icon.png' ), 6 );
}
public function execute(){
if($this->class){
$this->controller = call_user_func_array(array($this->class,$this->method), $this->args);
}
}
}
The requested class goes here
class Settings extends Grv{
public function __construct() {
//parent::__construct();
}
public function view($id=false){
$this->template = 'settings/view'; // using $this here craetes a fatal error
$this->render();
}
}
suppose i requested
admin.php?page=grv&c=settings/view/2
please tell me what i am doing wrong..
use $this->render() instead of $this->class->render();
another is: add ucfirst because your class name is in first letter capital and you are passing small case in arguments.
$this->controller = call_user_func_array(array(ucfirst($this->class), $this->method), $this->args)

Using the singleton method to create a global object

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.

Categories