Simple OOP question refactoring a function

Alles, was PHP betrifft, kann hier besprochen werden.

Simple OOP question refactoring a function

Postby ic3cold » 17. April 2010 18:36

I have a function I need to insert into my class as a method, but as a newbie to OOP, I am still not sure if I used the pseudo var $this properly, as I am confused where to use it inside the function in my class. For instance: I have this code


Code: Select all
function process_button() {
    global $_POST, $products, $cart;
    $this->$products = $_SESSION['cart']->get_products(); // I am not sure if this is the right syntax, I want to check $_SESSION['cart'] and call get_products()    function, to retrieve the list of products in the shopping cart.

    for ($i=0, $n=sizeof($this->$products); $i<$n; $i++) {
 

    $this->$productsImage = (IMAGE_SHOPPING_CART_STATUS == 1 ? zen_image(DIR_WS_IMAGES . $this->$products[$i]['image'], $this->$products[$i]['name'], IMAGE_SHOPPING_CART_WIDTH, IMAGE_SHOPPING_CART_HEIGHT) : ''); // This checks the cart status and retrieves the image location using the function zen_image 
    $this->$quantityField = zen_draw_input_field('cart_quantity[]', $this->$products[$i]['quantity'], 'size="4"');// quantity in cart
    $this->$productsPrice = $this->$currencies->display_price($products[$i]['final_price'], zen_get_tax_rate($this->$products[$i]['tax_class_id']), $this->$products[$i]['quantity']) . ($this->$products[$i]['onetime_charges'] != 0 ? '<br />' . $this->$currencies->display_price($products[$i]['onetime_charges'], zen_get_tax_rate($this->$products[$i]['tax_class_id']), 1) : ''); // This checks for additional charges and taxes
    $this->$productsPriceEach = $this->$currencies->display_price($this->$products[$i]['final_price'], zen_get_tax_rate($this->$products[$i]['tax_class_id']), 1) . ($this->$products[$i]['onetime_charges'] != 0 ? '<br />' . $this->$currencies->display_price($this->$products[$i]['onetime_charges'], zen_get_tax_rate($this->$products[$i]['tax_class_id']), 1) : '');// Calculates the individual product final price
// function continues ...
}


Now I am getting error
Fatal error: Call to a member function get_products() on a non-object in ... line 92
Line 92 corresponds to
Code: Select all
 $products = $_SESSION['cart']->get_products();
ic3cold
 
Posts: 2
Joined: 16. April 2010 22:26

Re: Simple OOP question refactoring a function

Postby Nobbie » 17. April 2010 22:39

ic3cold wrote:Now I am getting error
Fatal error: Call to a member function get_products() on a non-object in ... line 92
Line 92 corresponds to
Code: Select all
 $products = $_SESSION['cart']->get_products();


Das ist aber auch wirklich Horrorcode.

Man kann das aber möglicherweise retten, zunächst müßtest Du erklären, wie $_SESSION['cart'] überhaupt gespeichert wird (wie es erzeugt wird und was der Inhalt ist). Insbesondere gilt es zu berücksichtigen, dass man nicht einfach Objektve in Sessionvariablen abspeichern kann, dazu gibt es die Funktionen serialize() und unserialize(). Ich bin aber nicht sicher, ob diese Funktionen auch Methoden sichern (früher wurden nur Attribute gesichert), das müßte man erst einmal herausfinden.
Nobbie
 
Posts: 6469
Joined: 09. March 2008 13:04

Re: Simple OOP question refactoring a function

Postby ic3cold » 17. April 2010 23:12

Nobbie wrote:
ic3cold wrote:Now I am getting error
Fatal error: Call to a member function get_products() on a non-object in ... line 92
Line 92 corresponds to
Code: Select all
 $products = $_SESSION['cart']->get_products();


Das ist aber auch wirklich Horrorcode.

Man kann das aber möglicherweise retten, zunächst müßtest Du erklären, wie $_SESSION['cart'] überhaupt gespeichert wird (wie es erzeugt wird und was der Inhalt ist). Insbesondere gilt es zu berücksichtigen, dass man nicht einfach Objektve in Sessionvariablen abspeichern kann, dazu gibt es die Funktionen serialize() und unserialize(). Ich bin aber nicht sicher, ob diese Funktionen auch Methoden sichern (früher wurden nur Attribute gesichert), das müßte man erst einmal herausfinden.


Probably it is as I am a total newb in OOP! Sorry for that!
serialize() and unserialize()? What are they good for?

My problem is my class is not giving any feedback if I place it where it should go ... it returns a blank page!

Danke schon ...
ic3cold
 
Posts: 2
Joined: 16. April 2010 22:26



Return to PHP

Who is online

Users browsing this forum: No registered users and 3 guests