TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-01-2009, 01:01 PM   #1 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Bricks Call a function from class 2, inside class 1

Hello! I would like to know the easiest, or best way to do that; call a function from another class inside a class. Both classes are of course included in the page where I want the output.

How should I start learning to do this? Which is the best method for a new to classes guy like me? :)

Mostly it'll just be that I want to connect and query to the MySQL via. my database class, from my, for instance, News class.
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote
Old 06-01-2009, 04:45 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Something like this, probably:

PHP Code:
class TestClass
{
    protected 
$libraries = array();
    
    public function 
using($name$obj)
    {
        
$this->libraries[$name] = $obj;
    }
    
    public function 
someMethod()
    {
        return 
$this->libraries['db']->query('some query here');
    }
}

// Example
$db = new PDO(...); // create a new db connection...etc, etc.

$myObject = new TestClass();
$myObject->using('db'$db);

var_dump($myObject->someMethod()); 
This way, you could have all the objects that you want to use at your disposal.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Next class project? allworknoplay The Lounge 6 04-18-2009 08:33 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM


All times are GMT. The time now is 11:09 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design