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 01-26-2009, 11:00 AM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2008
Location: Cloud 9
Posts: 19
Thanks: 0
Jenski is on a distinguished road
Help How to call classes?

I have this setup:

class a
class b extends a

class c
class d extends c


I want class d to be able to call several functions in class b.
What is the best way of doing this?
Jenski is offline  
Reply With Quote
Old 01-26-2009, 02:20 PM   #2 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

either pass class b to class d by reference or just have class d create a class b within it.
CoryMathews is offline  
Reply With Quote
Old 01-26-2009, 09:26 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I would pass it as a reference.

PHP Code:
class extends c
{
private 
$classB;

public function 
setClassB($class)
{

if(
is_object($class)
{
$this->classB $class;
}

}


PHP Code:
$b = new classB();
$d = new classD();
$d->setClassB($b); 
Or you could design it to be added in the __construct function so you would simply use this:
PHP Code:
$b = new classB();
$d = new classD($b); 
And to use the class b functions inside your class d, you just do like this:
PHP Code:
$result $this->classB->function($args); 
__________________
Tanax is offline  
Reply With Quote
Old 02-04-2009, 10:22 AM   #4 (permalink)
The Wanderer
 
Join Date: Apr 2008
Location: Cloud 9
Posts: 19
Thanks: 0
Jenski is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post

Or you could design it to be added in the __construct function so you would simply use this:
PHP Code:
$b = new classB();
$d = new classD($b); 
And to use the class b functions inside your class d, you just do like this:
PHP Code:
$result $this->classB->function($args); 
Could you create it in the __construct function? Instead of passing, or would that not work? e.g.

PHP Code:
class classD
{
private 
$classB;

public function 
__construct()
{
require_once(
'/path/to/classB/file');
$this->classB = new classB;
}  

$d = new classD(); 
Jenski is offline  
Reply With Quote
Old 02-04-2009, 11:53 AM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Yes you can, but I would not advise it.
Sometimes the classes you want to include(for example a DB class), you need to set that class up with some basic values, in this example(the db class) you need to connect and select a db before you can use it. And if you have it instanciated in the __construct, you need to pass all the DB values to that class.

Not to mention, it will be harder for you to use it in another class.
It's easier to pass it as a reference, because then you can set it up before you pass it, plus you can pass it to several different classes.

I'm not saying that my solution is the best in your case, but generally, you want to do how I said. Not good to learn bad habbits, they're so hard to get rid of.
__________________
Tanax 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
Abstract Classes sketchMedia Advanced PHP Programming 18 02-28-2013 05:38 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Sibling Classes not getting along trs21219 Advanced PHP Programming 3 04-27-2008 03:57 AM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Classes: Function Usage ReSpawN Advanced PHP Programming 8 01-03-2008 05:41 PM


All times are GMT. The time now is 04:55 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