Welcome Guest, Not a member yet? Register   Sign In
Begining CI / Frameworks in General
#3

[eluser]esra[/eluser]
The Blog function is your constructor for PHP4 only. You can use a PHP5 style constuctor function when using PHP5. The constructor is particularly useful if you write a base controller and create new controllers by extending the base controller. You can add code to the constructor that you want the child controllers to globally inheirit. For example, a base controller...

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

class Application extends Controller
{
    function Application()
    {
        parent::Controller();
        $this->load->library('Btemplate');
        $this->load->library('Breadcrumb');
        $this->breadcrumb->set_delimiter('»');
        
    }

    function Index()
    {
         // controller-specific construction code

    }
}
?>

Now a sibling controller based on the Application Controller...

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

class Blog extends Application
{
    function Blog()
    {
        parent:Application();

    }

    function Index()
    {
        // controller-specific constructor code

    }
}

Another sibling controller...

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

class Articles extends Application
{
    function Articles()
    {
        parent:Application();

    }

    function Index()
    {
        // controller-specific constructor code

    }
}

With OOP, you want to create parent classes which have methods to pass on to sibling classes. This minimizes the amount of code you need to write in your sibling controllers. In the above, the Blog and Articles controllers would inheirit the operations included in the Application controllers constructor. For example, Articles and Blog would be able to access the methods (functions) in the Btemplate and Breadcrumb library, loaded by the parent Application controller.

After CI processes the constructor code, the Index function is processed if one exists. This allows you to execute controller-specific construction code. That is, the code in the Index function of your Application controller would be overwritten by whatever code existed in the Index function of the Blog and Articles controllers.

There are some nice tutorials on the web about object oriented programming with PHP. Some include comparisons that show how procedural code is rewritten to take advantage of OOP. You might do some searches for these. For some additional searches, try 'php design patterns'. Design patterns allow you to further modularize your code into objects and minimize code redundancy.


Messages In This Thread
Begining CI / Frameworks in General - by El Forum - 07-03-2007, 10:41 PM
Begining CI / Frameworks in General - by El Forum - 07-03-2007, 11:50 PM
Begining CI / Frameworks in General - by El Forum - 07-04-2007, 01:24 PM
Begining CI / Frameworks in General - by El Forum - 07-04-2007, 06:01 PM
Begining CI / Frameworks in General - by El Forum - 07-04-2007, 06:32 PM
Begining CI / Frameworks in General - by El Forum - 07-04-2007, 06:37 PM



Theme © iAndrew 2016 - Forum software by © MyBB