Click to See Complete Forum and Search --> : PHP drives me nuts -> Call to a member function on a non-object


AlionSolutions
November 25th, 2004, 11:26 AM
Hi again,
yes, this sucks ! The code fragment below is a stripped down version of a part of my current project.


class someobject
{
function anyfunction(){}
}

class someclass
{
var $someobject;

function someclass()
{
$someobject = new someobject();
}

function getobject()
{
return $this->someobject;
}
}


$obja = new someclass();
$obja->someobject->anyfunction();


Yess, and at the call of anyfunction, PHP reports:
Call to a member function on a non-object

I do not understand this. Does anybody know, why this happens and what I can do to make it work ?

Thanx in advance

alionsonny

khp
November 25th, 2004, 12:53 PM
Your constructor for someclass is not setting the someobject member variable correctly, so it remains a null reference, which causes the error.

Correct the constructor to.


function someclass()
{
$this->someobject = new someobject();
}

AlionSolutions
November 25th, 2004, 02:07 PM
Yeah thanx !
As I come from C++ I am not used in putting this in front of members inside the class.

Thanx

alionsonny

lisk
November 2nd, 2005, 11:17 PM
I am getting the same error, but I don't think the fix here applies. Here is my code:

} else {

$product_info_query = tep_db_query("select p.products_id, pd.products_name, pd.products_description, p.products_model, p.products_quantity, p.products_image, pd.products_url, p.products_price, p.products_price1, p.products_price2, p.products_price3, p.products_price4, p.products_price5, p.products_price6, p.products_price7, p.products_price8, p.products_price1_qty, p.products_price2_qty, p.products_price3_qty, p.products_price4_qty, p.products_price5_qty, p.products_price6_qty, p.products_price7_qty, p.products_price8_qty, p.products_qty_blocks, p.products_tax_class_id, p.products_date_added, p.products_date_available, p.manufacturers_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");


$product_info = tep_db_fetch_array($product_info_query);

tep_db_query("update " . TABLE_PRODUCTS_DESCRIPTION . " set products_viewed = products_viewed+1 where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and language_id = '" . (int)$languages_id . "'");
$pf->loadProduct((int)$HTTP_GET_VARS['products_id'], (int)$languages_id);
$products_price=$pf->getPriceString();
if (tep_not_null($product_info['products_model'])) {
$products_name = $product_info['products_name'] . '<br><span class="smallText">[' . $product_info['products_model'] . ']</span>';
} else {
$products_name = $product_info['products_name'];
}

khp
November 3rd, 2005, 06:27 PM
I would suspect it's because $pf has not been properly set. That's about as detailed an answer I can give you, given what you have posted.

lisk
November 3rd, 2005, 07:36 PM
Sorry - I don't really know what to post. Does this help?

I am trying to implement this add-on for osCommerce:

Other than the code I pasted, $pf appears:

in products.php


<?php echo TEXT_ENTER_QUANTITY . ":" . tep_draw_input_field('cart_quantity', $pf->adjustQty(1), 'size="6"'); ?>

in application_top.php

// include the price formatter for the price breaks contribution
require(DIR_WS_CLASSES . 'PriceFormatter.php');
$pf = new PriceFormatter;

in shopping_cart.php

}

function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
global $new_products_id_in_cart, $customer_id;
global $languages_id;

$pf = new PriceFormatter;
$pf->loadProduct($products_id, $languages_id);
$qty = $pf->adjustQty($qty);
$products_id = tep_get_uprid($products_id, $attributes);
if ($notify == true) {
$new_products_id_in_cart = $products_id;
tep_session_register('new_products_id_in_cart');
}

100 or so lines down:

}

function calculate() {
global $languages_id;
$this->total = 0;
$this->weight = 0;
if (!is_array($this->contents)) return 0;
$pf = new PriceFormatter;
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
$qty = $this->contents[$products_id]['qty'];

// products price
if ($product = $pf->loadProduct($products_id, $languages_id)){
$prid = $product['products_id'];
$products_tax = tep_get_tax_rate($product['products_tax_class_id']);
$products_price = $pf->computePrice($qty);
$products_weight = $product['products_weight'];

...

function get_products() {
global $languages_id;

if (!is_array($this->contents)) return false;
$pf = new PriceFormatter;
$products_array = array();
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
if ($products = $pf->loadProduct($products_id, $languages_id)) {
$products_price = $pf->computePrice($this->contents[$products_id]['qty']);

in product_listing.php:

case 'PRODUCT_LIST_PRICE':
$lc_align = 'right';
$pf->parse($listing);
$lc_text = $pf->getPriceStringShort();

about 50 lines down:

case 'PRODUCT_LIST_PRICE':
$lc_align = 'right';
$pf->parse($listing);
$lc_text = $pf->getPriceStringShort();
break;

khp
November 5th, 2005, 06:40 AM
I don't have nearly enough information to give you a definite answer. But I would tend to suspect you need to add the line

$pf = new PriceFormatter;

before

$pf->loadProduct((int)$HTTP_GET_VARS['products_id'], (int)$languages_id);
$products_price=$pf->getPriceString();

At least that's what they do before calling loadProduct in the other files you posted.

Ofcourse the above advice could just as well be completly false, given that I don't have a clue what any of this does.

lisk
November 5th, 2005, 06:54 AM
Thank you for the suggestion! I did this and now it says Fatal error: Cannot instantiate non-existent class: priceformatter. Any ideas?

khp
November 6th, 2005, 07:00 AM
You need something like the line
require(DIR_WS_CLASSES . 'PriceFormatter.php');
At the top of your php file to include the priceformatter class.
If you just copy this it will most likely fail because DIR_WS_CLASSES is not defined.

But I would strongly suggest that you go learn some basic PHP, read some tutorials and books, before you go any futher with this.

ikanku15
June 4th, 2007, 08:17 PM
Hi all, i m a newby in this php thing. i also got the same problem:

Fatal error: Call to a member function on a non-object in
/var/www/somedir/somename.php on line 153

line 153: mysql_real_escape_string($this->incident->startDateTime->GetTimestamp(), $this->dao->GetDBConn()),

can anyone help me on this..

PeejAvery
June 5th, 2007, 08:55 AM
Welcome to the forums ikanku15. Please remember that when you have a problem, you should create a new thread and then post your problem there. Please do not post over older problems. I will ask a moderator to split this here.

Concerning your problem...on of the variables in the following line has not yet been processed. Look back through your code and you will find that one or more of these do not exist at the time of this line's execution.

$this->incident->startDateTime->GetTimestamp(), $this->dao->GetDBConn()
Is this your own code or something that someone else has done?