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
OOP. Object as a variable of other class.
Old 02-19-2010, 05:27 AM OOP. Object as a variable of other class.
Banned

Posts: 2
Name: Alexander Lukjanenko
Location: Ukraine
Trades: 0
Hi,

I have class "database" which connect to db. This class have method "sql" which return the result of sql query.
PHP Code:
class Database
{
        
//connect to server, select db
        
function __construct() {
        ...
        }

        
//run sql
        
function sql($sql_query) {
        ...
        }

Now I want to use object of database as variable in other class and get error.
PHP Code:
class Webpage
{
        
//connect to server, select db
        
function __construct() {
         ...
        
$this->db = new database($params);
         }
          function 
write_log() {
         ...
        
$this->db->sql("insert ....");
         }

How can I use object database as parameter of other class?
alexander.lukja is offline
Reply With Quote
View Public Profile Visit alexander.lukja's homepage!
 
 
Register now for full access!
Old 02-19-2010, 01:39 PM Re: OOP. Object as a variable of other class.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Did you include your database class? If not you may want to look into autoloading:
http://php.net/manual/en/language.oop5.autoload.php
__________________

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!
 
Old 02-21-2010, 02:35 AM Re: OOP. Object as a variable of other class.
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
And, if Matt's suggestion doesn't get you going, then tell us the error.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-22-2010, 11:31 AM Re: OOP. Object as a variable of other class.
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Just a sidenote, instead of your current pattern of development, you may want to consider making the $db parameter a class field of Database, then using Webpage to extend Database, calling the parent constructor within the class constructor so that the database is already initialized within Webpage with little additional code needed.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to OOP. Object as a variable of other class.
 

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.75091 seconds with 12 queries