Following the documentation as far as pulling the CI instance into my custom library doesn’t seem to work unless I do this.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class Property {
public function __construct() {
log_message('debug', 'Property class initialized.');
}
/**
* returns the property's address
* @param $id int
* @return string
*/
public function getAddress($id) {
$CI =& get_instance();
return $CI->Properties_model->get_property_address($id);
}
/**
* returns the property's main photo path
* @param $id int
* @param $size string
* @return string
*/
public static function getPhoto($id, $size = FALSE) {
$CI =& get_instance();
$photos = $CI->Photos_model->get_property_photos($id);
$path = FALSE;
if ($photos->num_rows() > 0) {
$path = $photos->row()->path;
if ($size) {
$path = str_replace('.jpg', $size . '.jpg', $path);
}
return $path;
} else {
return FALSE;
}
}
/**
* returns a set of quick facts beds, baths, price range
* @param $id int
* @return string
*/
public static function getQuickFacts($id) {
$CI =& get_instance();
$query = $CI->Properties_model->get_quick_facts($id);
$quickFacts = FALSE;
foreach ($query->result() as $row) {
$quickFacts = $row->bedrooms . ' bedrooms, ' . $row->full_baths . ' full baths' . (($row->half_baths > 0) ? ', ' . $row->half_baths . ' half baths' : '');
}
return $quickFacts;
}
}
/* End of File: Property.php */
/* Location: ./application/libraries/Property.php */What am I doing wrong here? I know that I shouldn’t need to declare CI in every function…
I tried putting in in the __construct both as $CI and $this->CI but in either case I get a “not defined” error.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.