Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts

    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;


  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    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.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Is it wise for me to just simply use a global?

    include ('crud');

    $db = new database();

    class BlogPost
    {
    global $db

    }

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •