well that declaration is an invalid one. __construct is a magic method that runs when an object of a class is instantiated. but as a magic method, it is public static rather than private, but you only really need to write 'function __construct(){}' because as a magic method the access level is already predetermined. When you are instantiating an object that has properties that need to be set, the __construct() method is the place to do that, or to call an initialization method. Here's an example of a typical constructor i use in a lot of classes.
Code: Select all
function __construct($data = array()) { //receives an array of data to populate the parameters
if (!is_array($data) == TRUE) {
return false; //returns false if array isn't passed
}
if (count($data) > 0) {//step through the array
foreach ($data as $name => $value) {
$this->__SET($name, $value);//my __set function verifies that the property exists, and sets it to the passed value if it does
}
}
$db = Database::getInstance();
if (isset($this->id)){//assuming the object was instantiated from an array so already exists in db
$sql = "SELECT
t.descr as title,
l.descr as location,
concat_ws(' ',u.first_name,u.last_name) as reports_to,
d.descr as department,
st.descr as status
FROM
job j inner join job_locations l ON j.location_id=l.id
INNER JOIN job_departments d on j.department_id=d.id
INNER JOIN users u on j.reports_to_id = u.id
INNER JOIN job_titles t ON j.title_id = t.id
INNER JOIN job_status st ON j.status_id = st.id
WHERE j.id=" . $db->quote($this->id);
$result = $db->query($sql);
if (!$result || $result->rowCount()==0){
return false;//no records found
}//this sets the relevant properties based on the result of the query above
$results=$result->fetch(PDO::FETCH_ASSOC);
$this->title = $results['title'];
$this->department = $results['department'];
$this->location = $results['location'];
$this->reports_to = $results['reports_to'];
$this->status = $results['status'];
}
return $this;//$this is now a valid object with all fields populated. give it back
}