New to programming and trying to understand it all. |
[eluser]jjcalleiro[/eluser]
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. 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.
[eluser]xwero[/eluser]
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.
[eluser]Rabbi[/eluser]
I am new in CodeIgniter framework. I read the user_guide from CodeIgniter Site but it doesn't work for me. 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. http://localhost/ci/index.php/blog/ 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. Thnx
[eluser]xwero[/eluser]
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.
[eluser]jjcalleiro[/eluser]
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. -Brandon
[eluser]jjcalleiro[/eluser]
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.. Code: class Parent_class Maybe this helps. -Brandon [code]
|
Welcome Guest, Not a member yet? Register Sign In |