• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

PHP5

ChrisR

Active member
#1
Ill start off by saying i think this would be the place to post this ;) Also if this is not meant to be posted on this forum feel free to delete it :p

Well i am working on a private project (not a forum but something for my self) and i am coding in php5 i know how to do basic OOP in php4 but i am trying to learn a lot about php5 and there are something i am having issues with finding on the web. ill do it in points.

(note this is not really code just mockup to try and show you what i mean)

1) Would this be logical in making a database layer.

PHP:
abstract class driver {

protected function connect (perms here) {
mysql_connect()
}

}

class db extends driver {

public function connect(perms here) {

parent::connect(perms here);

}
if its not logical what would be the best way to go about it?

2) when do you use static/final etc.. keywords as in my coding i have never seen the need for it or whens it best to use it.

3) When needing to use other classes in a class is it best to do it this way or is there a better way?

PHP:
class foo {

private $see = NULL;

public function loo() {

global $see;

$this->see = &$see;

}

}

class see {

code here ....

}

$see = new see;
So that's all i want to know right now also if you have any other tips for coding in php5 oo i am all ears.
 
#2
I suggest you learn some design patterns before you go about doing this although you can learn by doing.

I suggest creation a factory class to determine which db class to instantiate.
 

Eric

Active member
#3
1.) I'll skip, as I don't normally worry about abstract classes =)

2.) The final keyword simply prevents child classes from overriding a method (more here: http://php.net/manual/es/language.oop5.final.php ) - and static can be useful for things such as:

PHP:
class someClass
{
    private static $instance;

    private function __construct() {}

    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }
}

$class = someClass::getInstance();
3.) I usually do something like (off the top of my head so not perfect or anything :p ):

PHP:
class someClass
{
    private static $instance;

    private $dbobj = null;

    private function __construct() {}

    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function init($obj)
    {
        $this->dbobj =& $obj;
    }
}

class dbClass
{
    private static $instance;

    private function __construct() {}

    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function init()
    {
        // blah blah blah
    }
}

$db = dbClass::getInstance()->init();
$class = someClass::getInstance()->init($db);
 

ChrisR

Active member
#4
Okay i never got how the hole thing with getInstance. would it not be easier and use less code to do something like this.

PHP:
class dbClass
{

    private function __construct() {
         // do some code like connect
    }

    public static function init()
    {
        // blah blah blah
    }
}

$db = dbClass::init();
The only thing i can think of for the getInstance is so you do not have to put static on every function in that class or something?