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 07-19-2009, 10:11 PM   #1 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default The correct way to use multiple classes?

Is this even close to the "correct" way to do this?

main-class.php
PHP Code:
class main_class {
    private 
$someVar "Hello";
    public function 
talk($text) {
        echo 
$text;
    }

    public function 
dispBoard($boardID) {
        
$sql "SELECT * FROM " $config['mysql']['db-name'] . ".threads WHERE board-id =" $boardID "ORDER BY date";
        
$result mysql_query($sql);

        
// Then display the board.....
    
}

mysql-class.php
PHP Code:
class mysql_class extends main_class {
    protected function 
connect($host$username$password) {
        return 
mysql_connect($host$username$password);
    }


string-class.php
PHP Code:
class string_class extends main_class {
    public function 
trim($str) {
        return 
trim($str);
    }



What I'm trying to do is be able to call something like this
PHP Code:
$system->mysql_connect($config['mysql']['host'], $config['mysql']['username'], $config['mysql']['password']);
$system->dispBoard(12); // The board ID 
It would require more code than that, I didn't feel like writing it all for this post, but is that about the right way to do this? I want the separate "jobs" to be in separate files.

Should I just combine it all into one big file?
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 07-19-2009, 10:18 PM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Nay, you're looking now into design patterns, for this sort of occasion perhaps a Registry pattern would suffice


Note: Technically speaking you COULD but it's just bad practice.

php Code:
<?php

final class Registry
{
    private static $lib;
   
        public static function fetchLibrary($lib)
        {
            if( !array_key_exists($lib, self::$lib) )
            {
                self::$lib[$lib] = new $lib;
            }
           
            return self::$lib[$lib];
        }
}

class MySQL
{
    //Coool stuff here
}

class forumApp
{
    private $db, $session, $user;
   
        public function __construct()
        {
            $this->db      = Registry::fetchLibrary('MySQL');
            $this->session  = Registry::fetchLibrary('session');
            $this->user     = Registry::fetchLibrary('user');
        }
}

Alternatively you could something like the following

php Code:
<?php

class MySQL
{
    //Coool stuff here
}

class forumApp
{
    private $db, $session, $user;
   
        public function __construct( MySQL $mysql )
        {
            $this->db     = $mysql;
        }
}

$mysql = new MySQL();
$fApp = new forumApp( $mysql )

Can't remember for the life of me what the pattern is called.
__________________
My Blog
Enfernikus is offline  
Reply With Quote
The Following 2 Users Say Thank You to Enfernikus For This Useful Post:
adamdecaf (07-19-2009), hello-world (09-17-2009)
Old 07-19-2009, 10:26 PM   #3 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

Ok, thanks.

I think it will be easier for me to just include everything into one file and one class. It will be more annoying for my 'ocd' but oh well...

Many thanks.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 07-20-2009, 12:32 AM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Why are you putting display code that is only going to be used in one file in a class? Objects are for core logic and code you want to re-use over and over again. The word "display" in a class almost always means you are doing it incorrectly.

The way I try to look at it is pretending that classes and objects have no access to any sort of HTTP output (echo, print, ect).

Enfernikus, your final way is probably the best method. Just pass the object by reference opposed to value so a new instance is not created.
__________________

Village Idiot is offline  
Reply With Quote
The Following 2 Users Say Thank You to Village Idiot For This Useful Post:
adamdecaf (07-20-2009), hello-world (09-17-2009)
Old 09-17-2009, 01:56 AM   #5 (permalink)
The Contributor
 
hello-world's Avatar
 
Join Date: Feb 2009
Posts: 73
Thanks: 30
hello-world is on a distinguished road
Default

I was searching to know something about Pattern. and this thread help me alot.
hello-world 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
abstract classes and constructors captainmerton Absolute Beginners 9 06-08-2009 06:36 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Passing multiple arguments to a function via one variable. delayedinsanity Advanced PHP Programming 10 05-07-2008 05:04 AM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM


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