Log in

View Full Version : Please explain the functionality of given code



aqeel
09-28-2010, 12:55 PM
hi friends The following code is working fine , but can anyone explain it plz that how this code is working i mean there is no function call how object employee set title as developer how it calls __set(...). Does this mechanism has any specific name in php.

class Employee{
var $name;
function __set($propName,$propValue){
$this->$propName = $propValue;
}
}
$employee = new Employee();
$employee->name = "aqeel";
$employee->title = "Developer";
echo "Name : {$employee->name}";
echo "<br />";
echo "Title : {$employee->title}";

The output is
Name : aqeel
Title : Developer

djr33
09-28-2010, 03:47 PM
A class can contain variables. You are just storing values in those variables then echoing them. In fact, I don't think it's even needed (the __set function).

traq
09-28-2010, 08:18 PM
it's a "magic" method - look here (http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members)

however, djr is correct - in this case, the __set() function is doing nothing (or at least, nothing that isn't being done anyway - but, per the page I linked above, I don't think it's doing anything at all). The class variables are being set directly ($employee->name = "aqeel"; $employee->title = "Developer";).

aqeel
09-29-2010, 01:39 AM
hay thanks friend , i checked it after commenting function __set(...) thanks with regards aqeel