Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Call to a member function query() on a non-object
Old 10-18-2008, 04:04 PM Call to a member function query() on a non-object
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Hello all,

Im working on a project and have hit a wall and cant figure it out

Im trying to make this project class orientated so all information is retrieved via class etc.

So i have my mysql class which i have used on almost every project i have done for ages, and i have my "config" file which i am using to include all classes like:
PHP Code:
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/mysql.class.inc.php');
$db = new database;
global 
$db
I then have another class which is included in exactly the same way after this (and database connect function) which connects to database using the mysql class.

and i am getting the

Fatal error: Call to a member function query() on a non-object in /home/USER/public_html/includes/show.class.inc.php on line 26

at the top of this file i have require config which i know is working as its using the show class, but cant seem to use the mysql class :s

and yet on another page i am using the same thing of using the mysql class functions within a class so i just dont get whats different :s any ideas.


Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 10-18-2008, 05:39 PM Re: Call to a member function query() on a non-object
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Thats a class, so you need to put -> try:
PHP Code:
$db->query(); 
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 10-18-2008, 05:50 PM Re: Call to a member function query() on a non-object
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
i am putting $db->query();

... i knew you would reply to this! lol... not on MSN or i would have asked instead of posting

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-18-2008, 07:21 PM Re: Call to a member function query() on a non-object
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
You need to instantiate your database class with the new operator $db = new database(); http://uk3.php.net/manual/en/language.oop5.basic.php

Also global has no meaning outside of a function. If you want to use the object $db within a function you need to declare global $db within that function.http://uk2.php.net/global

PHP Code:
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/mysql.class.inc.php');
$db = new database(); 

Last edited by maxxximus; 10-18-2008 at 07:22 PM..
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 10-18-2008, 08:08 PM Re: Call to a member function query() on a non-object
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
I dont think so =/

Okay i will give more code examples, but i dont want to post entire scripts becasue it is intended for (encoded) premium release.

/includes/config.php
PHP Code:
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/mysql.class.inc.php');
$db = new database;
global 
$db;
 
$db->connect();
 
include_once(
$_SERVER['DOCUMENT_ROOT'].'/includes/show.class.inc.php');
$show = new show;
global 
$show;
?>
/admin/showadd.php
PHP Code:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/includes/config.php');
 
$show->add('Dans Demo Show''Dan''Dan adds a show usign his show class for kicks''11:00:00''12:00:00''0''0''DanImage''DANDEMO');
?>
/includes/show.class.inc.php
PHP Code:
<?php
class show 
{
 public 
$title;
 public 
$presenter;
 public 
$time_start;
 public 
$time_end;
 public 
$info;
 public 
$image;
 public 
$contact;
 public 
$hidetime;
 public 
$ref;
 public 
$id;
 public 
$type;
 
 function 
add($title$presenter$info$time_start$time_end$hidetime$contact$image$ref)
 {  
  
$result $db->query("INSERT INTO `shows` SET ref='$ref', title='$title', presenter='$presenter', info='$info', time_start='$start_time', time_end='$time_end', hidetime='$hidetime', contact='$contact'");

 }
}
?>
Please advise the problem, as this structure is working fine for another class in the system.

Many Thanks
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-19-2008, 03:16 AM Re: Call to a member function query() on a non-object
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
You need to look back again at variable scope http://uk3.php.net/variables.scope

Variables defined within the main body of code (outside of all function and classes) have global scope. If a function needs to reference a variable defined in the global scope it must use the global keyword, otherwise it will only look in its local namespace.
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 10-19-2008, 06:07 AM Re: Call to a member function query() on a non-object
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay so why does it work in the other class?

PHP Code:
<?php
class page 
{
 public 
$ref;
 
 function 
content($ref)
 {
 
$result $db->query("SELECT * FROM `pages` WHERE ref='homepage' LIMIT 1");
 
$page $db->fetch_row($result);
 
$content $page['content'];
 
$content stripslashes($content);
 
 return 
$content;
 }
// END CLASS
?>
Included and used in same way, so why does this work but the other not?

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-19-2008, 06:07 PM Re: Call to a member function query() on a non-object
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Try:

PHP Code:
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/mysql.class.inc.php');
include_once(
$_SERVER['DOCUMENT_ROOT'].'/includes/show.class.inc.php');
$db = new database;
global 
$db;
 
$db->connect();
$show = new show;
global 
$show;
?>
and

PHP Code:
<?php
class page 
{
 public 
$ref;
 
 function 
content($ref)
 {
global 
$db;
 
$result $db->query("SELECT * FROM `pages` WHERE ref='homepage' LIMIT 1");
 
$page $db->fetch_row($result);
 
$content $page['content'];
 
$content stripslashes($content);
 
 return 
$content;
 }
// END CLASS
?>
PHP Code:
<?php
class show 
{
 public 
$title;
 public 
$presenter;
 public 
$time_start;
 public 
$time_end;
 public 
$info;
 public 
$image;
 public 
$contact;
 public 
$hidetime;
 public 
$ref;
 public 
$id;
 public 
$type;
 
 function 
add($title$presenter$info$time_start$time_end$hidetime$contact$image$ref)
 {  
global 
$db;
  
$result $db->query("INSERT INTO `shows` SET ref='$ref', title='$title', presenter='$presenter', info='$info', time_start='$start_time', time_end='$time_end', hidetime='$hidetime', contact='$contact'");

 }
}
?>
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 10-19-2008 at 06:10 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 10-20-2008, 02:04 AM Re: Call to a member function query() on a non-object
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
Not a OOP guru but I'd say your going about this the wrong way.

Surely your classes that are using the query method should be inheriting it from the database class. So class show should be extending database.

Also the use of the global keyword to import a variable into local scope should be used sparingly as it passes the variable by reference rather than value.
PHP Code:
<?php
class show extends database
{
 public 
$title;
 public 
$presenter;
 public 
$time_start;
 public 
$time_end;
 public 
$info;
 public 
$image;
 public 
$contact;
 public 
$hidetime;
 public 
$ref;
 public 
$id;
 public 
$type;
 
 function 
add($title$presenter$info$time_start$time_end$hidetime$contact$image$ref)
 {  
  
$result $this->query("INSERT INTO `shows` SET ref='$ref', title='$title', presenter='$presenter', info='$info', time_start='$start_time', time_end='$time_end', hidetime='$hidetime', contact='$contact'");

 }
}
?>
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 10-20-2008, 03:07 AM Re: Call to a member function query() on a non-object
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by maxxximus View Post
Not a OOP guru but I'd say your going about this the wrong way.

Surely your classes that are using the query method should be inheriting it from the database class. So class show should be extending database.
I disagree. There are certain cases when inheritance should be used and those cases occur when one object is a subset of another. Cars and Trucks are both subsets of vehicles so if you have a car class and a truck class, then they should extend your vehicle class. A professor of mine explained it as an "is a" relationship (ie a car is a vehicle).

In this case, a database object, which contains the query method, is not a show, which needs the query method. Inheritance might work in this case but I don't think it is good practice. Personally I would just have the show class contain an instance of a database. If need be the database can be passed to the show via the constructor or an additional method if it is not the case that a new instance of a database is needed. But this is just what I think.

If a particular class needs an instance of another class, and it is not appropriate to create a new instance of that class then that class should be passed to it. I think the use of global is not the proper solution here.

PHP Code:
class show
{
     private 
$db;
     
__construct($db)
     {
          
$this->db $db;
     }

     
add(...)
     {
          
$this->db->query('...');
     }

Also, when I see a bunch of public member variables in a class I get the itch to moan about how that is bad in a lot of cases, but in light of my previous lecture about inheritance I'll save this one for later.

Hope that helps
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 10-20-2008 at 03:10 AM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-20-2008, 01:22 PM Re: Call to a member function query() on a non-object
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Thanks for the responses,

I used public because the variables will be accessible from out side of the script so all the variables at some stage accessed when the class is in use if that makes sense, i was always under the impression that was the correct way to use them

PHP Code:
class show 

     private 
$db
     
__construct($db
     { 
          
$this->db $db
     } 
</SPAN></SPAN>

I have seen this a couple of times but never sure how it works.

In the end i added global $db; in each function within the show class, which seems to work, but i still dont know why :s, because another class i have works find without havnt to re-globalise the $db =/

Its a bit weird... and only just realised im typing in green stupid code highlighting bugs!
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 10-20-2008, 02:35 PM Re: Call to a member function query() on a non-object
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You're correct that the variables will be accessible from outside of the class. You will also be able to overwrite them. The default scope for variables within a class is private for a reason. The "correct" way to do it would be to use private member variables and then use getters and setters for access. The only time I use public member variables is with nested classes.

But then again these rules are intended more for a software engineering environment where many people will be accessing the code. It only catches my attention because I was taught never to do it, that does not necissarily make it right.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to Call to a member function query() on a non-object
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.45864 seconds with 12 queries