Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-01-2010, 02:28 AM   PM User | #1
Dat
Regular Coder

 
Join Date: Oct 2007
Posts: 147
Thanks: 28
Thanked 0 Times in 0 Posts
Dat is an unknown quantity at this point
Class inside a class

I know this wont work but in a set of minds how can I get a class to be included inside a class? or how do i get a class to work inside a class?

PHP Code:
class BlogPost
{
include (
'database.php');
$db = new database;

Dat is offline   Reply With Quote
Old 04-01-2010, 02:33 AM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Initialise the database class before this one and pass it in when you initialise this class.

Code:
$blogpost = new BlogPost($db);

class BlogPost
{
    private $database = '';

    function __construct($database)
    {
        $this->database = $database;
    }
}

Last edited by MattF; 04-01-2010 at 02:38 AM..
MattF is offline   Reply With Quote
Old 04-01-2010, 03:34 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by MattF View Post
Initialise the database class before this one and pass it in when you initialise this class.

Code:
$blogpost = new BlogPost($db);

class BlogPost
{
    private $database = '';

    function __construct($database)
    {
        $this->database = $database;
    }
}
This is how I'd do it as well. I try to limit object coupling as much as possible, so passing a parameter in makes the most sense.
What I can suggest (when you're OO skill is a little more advanced) is to interface whatever is being passed in. This allows you to guarentee (minus a bug, I won't go into that) that all methods are available in the given class. This only works for a more pure OO approach, where the parameter is also an object:
PHP Code:
interface IStorage
{
    public function 
connect(....);
    public function 
select(....); 
    
//....
}

class 
XMLFileDriver implements IStorage
{
    public function 
connect(...)
    {
       
// Open a file, maybe a domdocument, whatever
    
}
    public function 
select(...)
    {
        
// yeah, actually the dom would be great for selections
    
}
}

class 
BlogPost
{
    private 
$database '';

    function 
__construct(IStorage $database)
    {
        
$this->database $database;
        
$this->database->connect(...);
    }
    public function 
getBlogPost($maybeAnIDHere)
    {
        
$qry $this->database->select(array('*'), 'Post'$maybeAnIDHere);
        
$sResult '';
        if (
$qry)
        {
            
$sResult $this->database->fetch($qry); // Assume we have a fetch
        
}
        else
        {
            
$sResult 'No match';
        }
        return 
$sResult;
    }
}

$blogPost = new BlogPost(new XMLFileDriver('/path/to/XML.xml'));
print 
$blogPost->getBlogPost(1); 
Does that make sense? Its a little trickier to follow, but in the above we made a storage device, using an XML file for this example, selected from it and returned the blogPost. Now the above is a completely incomplete representation, its mearly to show the idea of datatyping (or TypeHinting in PHP) to ensure that the IStorage interface has the connect, select and fetch methods. It doesn't matter at this point what the IStorage represents, it can be flat, XML, databases, stdin, whatever you want.


Also, I thought this was refering to nested classes when I read the title:
PHP Code:
class A
{
    class 
B
    
{
    }

PHP does not support nested or inner classes.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 04-15-2010, 07:35 PM   PM User | #4
Dat
Regular Coder

 
Join Date: Oct 2007
Posts: 147
Thanks: 28
Thanked 0 Times in 0 Posts
Dat is an unknown quantity at this point
Is it wise for me to just simply use a global?

include ('crud');

$db = new database();

class BlogPost
{
global $db

}
Dat is offline   Reply With Quote
Old 04-15-2010, 07:50 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No, globals should only ever be used when the signature of a method cannot be changed.
PHP has the benefit of plain text interpreted code, but in a language like C with poor documentation thats using global variables, there is no easy way to determine what that variable name is supposed to be. And since global always requires that the names match, it makes it impossible to debug. Always avoid global unless absolutely necessary.

A situation where global is used where I would consider it valid is in set_error_handler. Say you want to log this to a file, but don't want to constantly open and close it when an error is created. Error handler signature for its function pointer is: int handler(int, string, [string, [int, [array]]]);. There is no way to add file handle into this function pointer, so the only option is to globalize it within the function you create itself.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:35 PM.


Advertisement
Log in to turn off these ads.