Call to a member function query() on a non-object
10-18-2008, 04:04 PM
|
Call to a member function query() on a non-object
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
10-18-2008, 05:39 PM
|
Re: Call to a member function query() on a non-object
|
Posts: 843
Name: Mike
Location: United Kingdom
|
Thats a class, so you need to put -> try:
__________________
My Blog/Site: Please login or register to view this content. Registration is FREE
|
|
|
|
10-18-2008, 05:50 PM
|
Re: Call to a member function query() on a non-object
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
10-18-2008, 07:21 PM
|
Re: Call to a member function query() on a non-object
|
Posts: 219
Name: Rob
Location: UK
|
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..
|
|
|
|
10-18-2008, 08:08 PM
|
Re: Call to a member function query() on a non-object
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
10-19-2008, 03:16 AM
|
Re: Call to a member function query() on a non-object
|
Posts: 219
Name: Rob
Location: UK
|
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.
|
|
|
|
10-19-2008, 06:07 AM
|
Re: Call to a member function query() on a non-object
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
10-19-2008, 06:07 PM
|
Re: Call to a member function query() on a non-object
|
Posts: 843
Name: Mike
Location: United Kingdom
|
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..
|
|
|
|
10-20-2008, 02:04 AM
|
Re: Call to a member function query() on a non-object
|
Posts: 219
Name: Rob
Location: UK
|
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'");
} } ?>
|
|
|
|
10-20-2008, 03:07 AM
|
Re: Call to a member function query() on a non-object
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
Quote:
Originally Posted by maxxximus
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
Last edited by NullPointer; 10-20-2008 at 03:10 AM..
|
|
|
|
10-20-2008, 01:22 PM
|
Re: Call to a member function query() on a non-object
|
Posts: 6,521
Name: Dan
Location: Swindon
|
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 <<
|
|
|
|
10-20-2008, 02:35 PM
|
Re: Call to a member function query() on a non-object
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
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.
|
|
|
|
|
« Reply to Call to a member function query() on a non-object
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|