I have a problem with a constructor in a class that i'm using as a controller

I want URLs to point to the controller then i will read the query string in the url. Based on the query string in the URL, i will decide to execute/run a given method which then can carry out certain steps and/or load a page.

The url takes the form of;

domain.com/folder/property_type.php?r=addrec

This is how the controller looks

<?php



foreach(glob("../model/*.php") as $filename) {
include_once($filename);
}


class Property_type_controller{


function __construct()
{


	$query_string = $_GET['r']; 	


	if ($query_string == "addrec")
	{
	$this->property_type_add();
	}
	else if ($query_string == "procadd")
	{
	$this->property_type_added();
	}
	else if ($query_string == "view")
	{
	$this->property_type_view();
	}

}


function property_type_add()
{ 
		header('HTTP/1.1 200 OK');
		header('Connection: close');
		include_once '../view/property_type_add.php';
}

}
?>

It should load the "property_type_add" page using the include but it fails to.

I get a blank page and even the browser's view source is blank.

Could you help me figure out the problem. Is this the right way to get a class constructor method to execute by default when accessed in PHP ?

    Do you make an instance of this class somewhere within your includes or how is the class executed?

    Also what happens if you echo the $query_string variable?

      The constructor will not be called until a new object is instantiated. IE:

      $prop_type = new Property_type_controller;
      

        Undrium - I do not make an instance of this class anywhere

        If i call the query_string variable (echo it out) without being in a class, it shows the value, but when i put it in a class, it does not.

        Derokorian - does that mean that even if the class has a constructor, i have to call it by creating an object?
        Does that then mean that i should put this code

        
        $prop_type = new Property_type_controller;
        

        At the top before the class starts?

        I think what confused or misled me is that if you look at Codeigniter controllers

        http://codeigniter.com/user_guide/general/controllers.html

        <?php
        class Blog extends CI_Controller {
        
        public function index()
        {
        	echo 'Hello World!';
        }
        }
        ?>
        
        

        There is no instantiation shown and yet when the controller is executed, it outputs that message.

        So i assumed it was the same thing when creating a controller with plain PHP without using an MVC framework like Codeigniter.

          I happen to use CodeIgniter now, and I know what you mean it can be confusing but code ignitor automatically instantiates the controller, and then calls the index function if no subfunction is specified in the url. I use modrewrite my my urls are example.com/controller/function but if i just load example.com/controller it automatically calls the index function.

          This may be confusing if you haven't tried to do OOP before using a framework (which I would suggest) as it makes you forget that the class isn't normally automatically loaded.

          Also you define $query_string in a function in your class, meaning only that function can access that specific variable. Giving you the flexibility to use the same variable in multiple functions with different values each time. For example in my models I use $sql to build all my queries however the variable is unique to each function. You could however use $this->query_string to make it any function in that class. Globals are the only way I know of to use a variable in any function in any class, however I've been warned many times that this is against MVC structure.

            Thanks for that, it clears up the confusion I had seeing Codeigniter using classes for constructors without seeing any instantiation, then when trying to use a controller class without a framework, it wasn't the same.

            You are right, using a framework first can mask up a lot of things as several processes seem to be automated.

              Write a Reply...