Jump to content
Killersites Community
Sign in to follow this  
MrSpock

Php Construct

Recommended Posts

I am following the tutorial on Object Oriented PHP (which is something I've always struggled to grasp) and I'm using __construct() throughout the tutorial. Can you explain it in layman's terms as I'm struggle to understand it?

 

For example:

 

	class employee extends person {
	function __construct($employee_name) {
		$this->set_name($employee_name);
	}
}

Why is the __construct() needed here?

 

Many thanks

 

Spock.

Share this post


Link to post
Share on other sites

__contstruct() just allows you a way to perform various actions when the object is first created. Exactly what it does depends on what you need... but it could set various variables, connect to the database and retrieve data, etc.

 

For example, take those two pieces of code:

 

class employee extends person {
               function __construct($employee_name) {
                       $this->set_name($employee_name);
               }
       }

 

class employee extends person {
               function __construct() {
               }
       }

The first would allow you to set the name of the employee when the object is first created:

 

$person = new Employee('Their Name');

The second doesn't have that ability, so you'd need to have a set_name() method and call that separately.

 

$person = new Employee();
$person->set_name('Their Name')

Hopefully that makes sense? A more "real world" example might be something like this... say you ran a business, and were creating some software that stored information about your employees. For example, you could create an Employee object and pass in the employee ID when that object is first created:

 

$employee = new Employee(1234);

if you have a _construct set up, immediately when that object is created, you could run code to access the database and automatically populate information about that person (name, birthday, weekly schedule, etc.). You could do that same thing without __construct... but the code would be cleaner and simpler with __construct. The more simple and concise your code is, the easier it is to maintain and understand.

Share this post


Link to post
Share on other sites

	class person {

	var $name;
		public $height;
		protected $social_insurance;
		private $pinn_number;

		function __construct($persons_name) {
			$this->name = $persons_name;
		}
		function set_name($new_name){
			$this->name = $new_name;
		}
		function get_name(){
			return $this->name;
		}
}

class employee extends person{
	function __construct($employee_name) {
		$this->set_name($employee_name);
	}
}

 

so to use the classes above, using __construct() allows access to set_name and get_name when I call a new object of employee?

 

I'm still struggling to grasp it.

Share this post


Link to post
Share on other sites

So in the classes above, __construct() in both Person and Employee allows you to set the name of the person or employee when you create the object. The __construct() is automatically called when the object is created.

 

The __construct() doesn't have anything to do with allowing or not allowing access to set_name() or get_name(). It simply allows you to set a name when the object is created.

Share this post


Link to post
Share on other sites

Hi,

 

I have a question also related to constructors in PHP.

I studied some JAVA for 3 months and now I am starting to learn some c#.

I noticed that in these languages, we can overload the constructor.

Then, recently I went back to PHP to do some web development and I noticed I couldn't find a way to overload the constructor.

 

I was just wondering what you do in PHP if you wanted to overload a constructor?

Edited by Stephenius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×