Welcome Guest, Not a member yet? Register   Sign In
New to programming and trying to understand it all.

Im running through the first Hello World tutorial here on the site. At the end of the Hello World screencast i add the the following code.

function Blog() {

Its not explained, but i would like to know the purpose of this as i see no difference in the output of the view.

Thanks in advance.

The function is a constructor which means it gets executed when the class is called.

The parent::Controller() line is to inherit the constructor of the Controller class. This is needed if you want to add things to the constructor of your extended class otherwise you overwrite the Controller class constructor and you are going to end up with errors.

It is explained in the user guide but it isn't that clear.

I am new in CodeIgniter framework. I read the user_guide from CodeIgniter Site but it doesn't work for me. Sad

I don't know why? I am using IIS server for my own pc.

I just tried with Controllers chapter from user_guide.

I coded according to the chapter but this link isn't working in my pc.

Where, ci="CodeIgniter" folder of mine.

It shows "No input file specified."

Also tried with this link: http://localhost/ci/index.php/blog/index

I changed URL in application\config\config.php for "localhost". Like this:
$config['base_url'] = "http://localhost/ci/";

Can anyone tell me why this is happening? How can i get correct result?? Please help me.


Rabbi you should ask your question in another topic now you are threadjacking which is confusing because now there are two topics, the original one and yours.

After saying that Do you have a controller named blog in the controllers directory? If you do could you show the code of the controller. Please respond in a new topic to prevent further threadjacking, thank you.

@Lab Technician Thanks, i get it now.

Was looking over this, and had the question of, Doesn't the class naturally inherit the Controller class by the class declaration including the extends keyword referring to it?

[eluser]Brandon Dickson[/eluser]
PHP uses a concept called 'overloading' to handle inheritance, so when you have a class that is inherited by another, the new class inherits all the properties and methods of the parent class, however, if a method or property is defined in the new class it 'overloads' the previous declaration.

In PHP the constructor method is just another method, so if you define it in the second class, nothing from the constructor in the first class gets called... unless you call parent::Class_name() (in PHP4 & 5) or parent::_construct() ( in PHP5 + )

In CI, the Controller's constructor method is used to create instances of other classes that are accessible from what ever controller is currently loaded ( $this->load->view() for example ), as well as calling a few other functions required for the system to work.

So if you create a constructor in your controller, you need to call parent::Controller(), inside it, to make sure that the required functions get called, and the required class instances are added to your controller.

Also note that if you don't define a constructor in a child class, the parent's controller is automatically called, when an instance of the class is created.


Ok, i think i get it. although the last sentence kinda confuses me. if you dont call the constructor, it gets called anyways? Sorry for all the questions, just trying to learn.

[eluser]Brandon Dickson[/eluser]
You never actually call the constructor, it gets called by php when a new instance of a class is created.

Let me see if this helps, i've been known to ramble..

class Parent_class
         function Parent_class(){
                /* this is only called when a new instance is created e.g. new Parent_class()*/
                $this->message = "I'm the parent class!";

class First_child extends Parent_class
         function First_child(){
               $this->message = "I'm the first child!";

class Second_child extends Parent_class
/* no constructor here... */

class Third_child extends Parent_class
        function Third_child(){
                parent::Parent_class() /* calling the constructor of the parent class */
                $this->new_message = "I'm the third child!";

$parent = new Parent_class();

/* outputs : I'm the parent class! */
echo $parent->message;

$first = new First_child();

/* outputs : I'm the first child! ( the constructor was overloaded ) */
echo $first->message;

$second = new Second_child();

/* output :I'm the parent class!
( there is no constructor for the second child class,
so the parent constructor is called )*/
echo $second->message;

$third = new Third_child();

/* output : I'm the parent class! ( because we called the parent constructor inside the child constructor )*/
echo $third->message;

/* output : I'm the third child! */
echo $third->new_message;

Maybe this helps.


Ok, i think i get it. Thanks!

Theme © iAndrew 2016 - Forum software by © MyBB