Welcome Guest, Not a member yet? Register   Sign In
Extending from my own controller
#11

[eluser]n0xie[/eluser]
You don't need to define anything. It will work automatically.

If you put the code in the config add these 2 controllers:
Code:
// test.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {

    function __construct()
    {
        parent::__construct();
    }

}

and

Code:
// test2.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test2 extends Test {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        echo 'hello';
    }
}

Then point your browser to http://domain.tld/index.php/test2

It should output 'hello'.
#12

[eluser]Agustín Villalba[/eluser]
Ok n0xie! But I have a question about that:
That function loads every controller at the system start up, but could be a problem with that, because it could cause an overloading for the server, because if there are many controllers the time while this function loads all the controllers could cause the application runs very slow, don't it? Excuse my poor English! Thank you very much!
#13

[eluser]n0xie[/eluser]
It only loads the controller it needs: http://php.net/manual/en/language.oop5.autoload.php
#14

[eluser]Agustín Villalba[/eluser]
Ok!! It's perfect! I didn't know that function in PHP 5! Sorry about that, and thank you for expend your time solving this problem!!
#15

[eluser]Phil Sturgeon[/eluser]
Hello head, meet my friend wall.
#16

[eluser]Agustín Villalba[/eluser]
what????
#17

[eluser]Colin Williams[/eluser]
Since when did including dependencies before a class become "inelegant?" I know you can use __autoload() to invent your own conventions, which is fine, but is including something really so terrible?
#18

[eluser]Agustín Villalba[/eluser]
Oh no no!! Not at all!! All what I mean is that by including files dependencies with "include_once" you have to do it before every class, which implies that you can forget some "include" and you have to repeat many code, but it isn't "inelegant" at all! Don't get me wrong please! You're right too!
#19

[eluser]Alface[/eluser]
[quote author="n0xie" date="1263925356"]You don't need to define anything. It will work automatically.

If you put the code in the config add these 2 controllers:
Code:
// test.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {

    function __construct()
    {
        parent::__construct();
    }

}

and

Code:
// test2.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test2 extends Test {

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        echo 'hello';
    }
}

Then point your browser to http://domain.tld/index.php/test2

It should output 'hello'.[/quote]

It show me this:
Fatal error: Class 'Test' not found in C:\xampplite\htdocs\projetos\forsite\system\application\controllers\test2.php on line 3

PHP Version 5.2.8
#20

[eluser]Phil Sturgeon[/eluser]
[quote author="Alface" date="1273058408"]It show me this:
Fatal error: Class 'Test' not found in C:\xampplite\htdocs\projetos\forsite\system\application\controllers\test2.php on line 3

PHP Version 5.2.8[/quote]

Did you add the __autoload()? It's not found because you haven't told PHP how to find it.




Theme © iAndrew 2016 - Forum software by © MyBB