简体   繁体   中英

Can't extend my Controller

I am trying to write an extension to my Controller class. The problem is I can't seem to figure out how..

I have the following class named test in which there is one function which simply returns aaaa and in in the same file, at the end, as my Controller :

class test extends Controller
    {
        function test()
            {
                parent::Controller();
            }

        function echoMe(){

            return 'aaaaaaaaaaaaaaaaa';
        }
    }

Within my Controller class I have a function which is the general output for a clients homepage. I'm trying to call the function echoMe from the extension above, but I keep getting

Call to undefined method Controller::echoMe()

Here is the client function (the call to echoMe() is right at the top):

function controller_client1($controlData = NULL)
    {
        echo $this -> echoMe();

        //as the client page is content from the xml, mmodel needs the page number
        $pageNumber = '';
        if(isset($_GET['number']))
            {
                $num = htmlentities($_GET['number']);
                if(ctype_digit($num) && $num >= 0)
                    {
                        $pageNumber = $num;
                    }
            }
        else{
            $pageNumber = 0;
        }
        //loading the page content
        $data = $this->model->model_loadXMLdata($pageNumber);


        if(!empty($controlData))
            {
                //check if there is any info in the control data sent
                foreach($controlData as $key => $value)
                    {
                        //add the info to the data array
                        $data[$key] = $value;
                    }
            }

        $this->load->load_clientHomePage($data);
    }

I know this is a very simple question. I've been trying to follow this guide, but something isn't clicking...

Could somebody please help? How can I call the function echoMe() from test?

I know how to write just a brand new class and call it, but I'm trying to learn how to extend properly and keep failing.

Am I meant to call the "test" from within the Controller somewhere?

In the config.php you set the prefix for the file you want to extend. So it should be My_test, unless you have changed this preset(displayed below)

/*
|--------------------------------------------------------------------------
| Class Extension Prefix
|--------------------------------------------------------------------------
|
| This item allows you to set the filename/classname prefix when extending
| native libraries.  For more information please see the user guide:
|
| http://codeigniter.com/user_guide/general/core_classes.html
| http://codeigniter.com/user_guide/general/creating_libraries.html
|
*/
$config['subclass_prefix'] = 'MY_';

Make sure you have the controller in the right folder (application/core in codeigniter 2.1.0) then you shouldn't have a problem. Hope that helps

here is my controller that i extend. The file is called My_Controller.php (creative I know)

<?php
class MY_Controller extends CI_Controller {

function __construct()
{
    parent::__construct();
//constructor code here
}

//Custom functions here
}

//sencond controller I extend in the same file
class MY_Admin extends CI_Controller {

function __construct()
{
//more custom stuff for admin stuff
}

//more custom functions for admin stuff
}

?>

Has you see I have two extended controllers in the same file. My extending code looks like this:

<?php
class home extends MY_Controller
{

and then just replace My_Controller with My_Admin if I want to extend the admin.

Try this (general idea, not CodeIgniter only):

$test = new test();
echo $test->echoMe();

Remember, test extends your Controller class, not the other way round - so you can't call your methods outside the test class/object instance.

Also, it might be a good idea to upgrade to a new version of CodeIgniter - beware of the new parent controller name, though.

EDIT

Ok, this should be enought to get you started - note that it's PHP5, not PHP4, so constructors are called __construct and not the class name:

class Controller {
    public $mainvar; 

    function __construct() {
        $this->mainvar = '<br />';
    }

    function echoMe() {
        return 'aaaaaaaaaaaaaaaaa';
    }
}

class Test extends Controller {
    function __construct() {
        parent::__construct();
    }

    function echoMeAgain(){
        return 'bbb';
    }
}

$test = new Test();

echo $test->echoMe();
echo $test->mainvar;
echo $test->echoMeAgain();

echoMe() function is defined in test class and not in Controller class. When controller_client1 function is called with the instance of Controller class, echoMe does not exist because it is not defined within Controller class.

The best way to achieve this is to create empty echoMe function in base Controller class. This way polymorphism works. When calling controller_client1 function from instance of test class, method from that class will be executed. Otherwise, method from base class will be executed.

I hope I didn't miss the point of the question :)

echo $this -> echoMe(); will fail because its created in the child(extended) class and your calling it in the parent class. The question is a little hard to understand.

abstract class Controller{
    public function __construct(){

    }

    public function echoMe($str){
        echo $str;
    }

}

class test extends Controller{
    public function __construct(){
       parent::echoMe('aaaaaaaaaa');
    }
}

I think the question has actually already been answered by Kosta, but there might be some misunderstanding at your side. So let me extend that by some example code:

class Controller {
    public function run() {
        $this->echoMe();
    }
}

class test extends Controller {
    public function echoMe() {
        echo "works";
    }
}

// This does NOT work, because Controller::echoMe does not exist
$controller = new Controller();
$controller->run();

// This DOES work, because $this will be instance of test, and
// test::echoMe exists and is callable.
$test = new Test();
$test->run();

"extends" does not mean, that the actual Controller class gets extended. The new class "test" just inherits every single method and property that is not declared "private" from the Controller class. The Controller class itself remains untouched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM