TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   The correct way to use multiple classes? (http://www.talkphp.com/advanced-php-programming/4757-correct-way-use-multiple-classes.html)

adamdecaf 07-19-2009 10:11 PM

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?

Enfernikus 07-19-2009 10:18 PM

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.

adamdecaf 07-19-2009 10:26 PM

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.

Village Idiot 07-20-2009 12:32 AM

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.

hello-world 09-17-2009 01:56 AM

I was searching to know something about Pattern. and this thread help me alot.


All times are GMT. The time now is 09:52 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0