Welcome Guest, Not a member yet? Register   Sign In
Extending a CI library, works on php5.3 localhost not on php5.2 on production server
#1

[eluser]alectrash[/eluser]
Hi There

I have been going round in circles with this for a good while and since its Friday though I would put on here for help. I HAVE ALREADY FOUND A SOLUTION TOO but not sure if its the right thing to do.

I have extended the Codeigniter cart class to have an extra function to retrieve the weight of items in the basket. I store the weight in an extra index in the cart array. Its all working on localhost but not on my production server.

I have a basket.php controller which calls a function in the My_Cart class to get the weight.

Code:
<?php

class Basket extends Controller {

  function __construct()
  {
    parent::Controller();    
    $this->load->model('basket_model');
    $this->load->helper('url');  
    $this->load->library('form_validation');
  }

  function index()
  {  
    echo $this->cart->weight();
  }
  
}

and then this is my MY_Cart.php file

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
*   Extenstion of cart class
*/

class MY_Cart extends CI_Cart
{
  
   function MY_Cart()
   {
      parent::CI_Cart();
   }
  
   function _update($items = array())
   {
      //existing cart _update code goes here with a few changes to update weight, tax, vouchers on basket etc
   }
        
   function _save_cart()
   {
     //existing _save_cart code goes here with a few changes to save weight, tax and other stuff
   }
  
   function weight()
   {
    return 10;
   }
}
?>

On localhost (WAMP) (PHP5.3) this works but on production server (linux) (php5.2.7) I get the following error:

Code:
Fatal error: Call to undefined method CI_Cart::weight() in /var/www/vhosts/somedomain.co.uk/subdomains/thewebapp/httpdocs/system/application/controllers/basket.php on line 14

I have simplified the code above by taking lots of other non related stuff out but this is essentially the code which is not working. The _save_cart() and _update() method are adaptations of functions already in the native CI cart class and I have changed these to extend the functionality of the basket. These work fine.

I have already found a solution which is to add an empty function for weight() in the actualy codeigniter cart.php class.

in system/libraries/Cart.php i have added in

Code:
function weight()
{
///empty function to get the associated function in MY_Cart.php to work.
}

In finding this solution does this mean that you can only extend functions that are already in the native CI class? or is it possible to extend a class and add new functions without declaring them in the class your extending also?

Any help/advice much appreciated.

Thanks

Matthew Fedak
#2

[eluser]danmontgomery[/eluser]
My_Cart.php should be MY_Cart.php
#3

[eluser]alectrash[/eluser]
Thanks noctrum,

Sorry that was a typo in my question. The class was already correctly named My_cart.php. I have now updated the above question.

Matt
#4

[eluser]alectrash[/eluser]
DOH!

Wait sorry I see what you meant now. I made this mistake earlier today extending router and realised straight away so no idea why I forgot this time. Cause I dev on windows i forget that sometimes its forgiving and lets My_Cart.php slip through and still work.

Cheers

Matt
#5

[eluser]Aken[/eluser]
If you're using CI 2.0+, the standard controller extension is extends CI_Controller, and you'll want to be more consistent with your constructor function naming.
Code:
<?php

class Basket extends CI_Controller {

  function __construct()
  {
    parent::__construct();    
    $this->load->model('basket_model');
    $this->load->helper('url');  
    $this->load->library('form_validation');
  }

  function index()
  {  
    echo $this->cart->weight();
  }
  
}
#6

[eluser]alectrash[/eluser]
Lol, thanks but I'm not using CI 2.0 hence no proper __construct required and it extends just Controller. I will be looking to upgrade my project to CI2 asap though once complete. I am on a deadline to provide a working copy of my app soon so I have delayed the upgrade to CI 2 just to buy me some more time. After that I will upgrade straight away.




Theme © iAndrew 2016 - Forum software by © MyBB