We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Custom Libraries and CI

Development and Programming

SitesByJoe's avatar
SitesByJoe
163 posts
12 years ago
SitesByJoe's avatar SitesByJoe

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.