-1

I dont know if this is possible but when executing a function how do I know the name which object is going to be called after __construct through __construct.

<?

class SmallPortal extends CI_Controller {

    public function __construct(){

        parent::__construct();
        if($this->session->is_login()==true) {

        //put my sessions in an array

        } else { 

        //if not login page redirect to login page...

        }

    } //end __construct

    public function index(){

        //member page       

    }    

    public login(){

    //LOOP

    }

} 
?>
user1978142
  • 7,946
  • 3
  • 17
  • 20
ash
  • 101
  • 10
  • 3
    Do you mean what's the `parent` part of `parent::__construct()`? Use get_parent_class(). Or are you asking if construct knows about the next line of code that's going to get called after construct finishes? No.. it has no idea nor should it. – Mike B Jun 26 '14 at 11:48
  • Thanks for your quick replay, no i mean how do i know login is going to be called through __construct so i can prevent the loop. – ash Jun 26 '14 at 11:57
  • Well there are other options than to know which method has called the current method your in. It is possible but only on a [very hacky way](http://stackoverflow.com/questions/2110732/how-to-get-name-of-calling-function-method-in-php). But I can't see a recursion anyway. – TiMESPLiNTER Jun 26 '14 at 12:13
  • I guess to prevent the redirect loop its best to move the login object to a different class... – ash Jun 26 '14 at 13:52

1 Answers1

0

When you instantiate your new class and try and use it i.e.

$smallportal = new SmallPortal();

And then call the login method

$smallportal->login();

It will attempt to find that method in the $smallprotal object.

If it is found it will run it... end of story.

If the login() method does not exist in your object, it will automatically look up the heirarchy to its parent, and its parent and so on, and see if it can be found there.

As you have coded a login() method, if you want to run the parents login() method as well as yours you have to tell it to, like so.

public login(){

    parent::login();
    //LOOP

}

So if you code a login() method you are overriding any existing method that may exist in any parent object.

If you call the parent::login(); in your login() method you are sudclassing that method i.e. adding some more functionality to an existing process.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149