Welcome Guest, Not a member yet? Register   Sign In
How to use get_instance in PHP5?
#1

[eluser]Optimus Prime[/eluser]
Hi! I'm practicing in CI programming. I have a question about get_instance and how use it in PHP5. I want to use hooks to to check the uri at the pre_controller level.

I used this in the hook.php
Code:
$hook['pre_controller'][] = array(
                                'class'    => 'Testhook',
                                'function' => 'test',
                                'filename' => 'testhook.php',
                                'filepath' => 'hooks',
                'params'   => array()
                                );
in testhook.php:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Testhook{

var $CI;
var $urisegment;

    function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->helper('url');
        $this->urisegment = $this->CI->uri->segment(1);  //<< Only for test.
    }
    
    function test()
    {
        echo "Your uri in segment 1: ".$this->urisegment
    }
}

?&gt;
Suppose that to access to the welcoming message I type:

localhost/testsite/index.php

(all the site is contained in a folder called "testsite" and the C.I. folder is inside in "testsite")

But I get:
Code:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: hooks/Testhook.php
Line Number: 11

Fatal error: Call to a member function helper() on a non-object in /opt/lampp/htdocs/testsite/framework/application/hooks/testhook.php on line 11

What's wrong? I have read several posts about get_instance. Maybe I missed something about the get_instance. but I can't get it... I admit that i'm newbie with C.I. and advanced PHP5 Object Oriented Programming.

I'm using Fedora 7 with XAMPP to test this framework.

I will appreciate for your help.
#2

[eluser]Phil Sturgeon[/eluser]
instead of

Code:
$this->CI =& get_instance();

use

Code:
$this->CI = get_instance();

I believe PHP 5 automatically works out what you are trying to do. Whether it's the fastest way or not, it will stop that error message.
#3

[eluser]Optimus Prime[/eluser]
Hi thepyromaniac!

Thanks for your time for reading my post. Unfortunately, the error still appearing . =(. I tried that too.
#4

[eluser]Seppo[/eluser]
thepyromaniac you are right, but it won't help.
The problem is that the CI object is not created yet. You need to use a post_controller_constructor instead of a pre_controller.
#5

[eluser]NemetraL[/eluser]
As far as I know, the $CI instance is not available before the Controller is instanciated.
$CI is the main controller.

The only way to catch it through a hook is to use a 'post_controller_constructor' hook.
#6

[eluser]NemetraL[/eluser]
Sorry Seppo, I must have posted the reply at the same time than you ;-)
#7

[eluser]Optimus Prime[/eluser]
Hi Seppo and NemetraL!

Thanks for your time for reading my post. I think you are right, CI object was not created in the pre_controller stage, I tried in post_controller_constructor as you said. It seems to work fine! Thanks.

Well, based of your answer, clearly there is no way to create CI object as thepyromaniac said or the one that I posted at start before the post_controller_constructor. So, there is a way to ceck the uri without this object? Supposing that I want to check the uri using CI object before the controller are instantiated. I want that because I want to set a distinct uri behavior witouth using CI routing. I think that when the controller are instantiated they have a high priority in the uri behavior, or am I wrong?
#8

[eluser]NemetraL[/eluser]
What do you mean by "high priority"?
#9

[eluser]Optimus Prime[/eluser]
I mean that the Controller takes control about the uri. the normal uri behavior in CI is:

www.mysite.com/controller/function/parameter1/paramenter2/...../parametern

But, in some cases I don't want that the controller segment calls the controller class. I only want something like:

www,mysite.com/code

where "code" is a string that will select a determined controller, function and parameter. this code is variable, so configuring routes.php is not a good idea, and using _remap in the controlles sometimes causes conflicts whit the uri segments.

An example about what I mean is:

www.mysite.com/nike <-- No adding more uri segments.

if (code == "nike")
shows the content of www.mysite.com/products/shoes/nike (maintaining the addres, www.mysite.com}/nike)
elseif (code == "nintendo")
shows the content of www.mysite.com/products/consoles/nintendo (maintaining the addres, www.mysite.com/nintendo)

Well, this a single example, but i want to compare various "code" parameters with a database. So, there is the "why" of this topic. I want to check the uri segments before the controller takes controls over the uri segments behavior. If there is no code, so I check that the uri correspond the normal uri behavior, the testhook (or another method) will no do nothing and resume with the stages check order (post_controller_construct, post_controller, etc...).
#10

[eluser]Seppo[/eluser]
Routing will be good for you?

@NemetraL: I´m sure I beated you in almost a minute Tongue




Theme © iAndrew 2016 - Forum software by © MyBB