TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Sibling Classes not getting along (http://www.talkphp.com/advanced-php-programming/2710-sibling-classes-not-getting-along.html)

trs21219 04-26-2008 02:35 AM

Sibling Classes not getting along
 
ok here is my prediciment. i have just been recently working with php classes and oop.

i have a system class that i have contain all the other classes (they extend system)

Class System
  • database extends system
  • security extends system

how can i get the security and database classes to be able to access each other's functions without including each class file in each function that i want to use that class in

ex. i want to use
PHP Code:

$database -> query("query here"); 

from my security class without including database.class.php in each function i want to call something from the database in

if someone could shed some light on this i would really really appriciate it.

thanks^^

freenity 04-26-2008 11:00 AM

you should have a property in your security class that would be an instance of database class:

PHP Code:

class Security
{
    private 
$db;

    public function 
__construct()
    {
        
$this->db = new Database();
    }

    public function 
blabla
    
{
        
$this->db->query()....
    }


Also it would be better to have a method like getDb() that will return the database object in stead of accessing to the variable directly.

Wildhoney 04-26-2008 04:38 PM

Freenity is right. Well, not right, but it's also my most favourite way of implementation. Obviously there are occasions where inheritance is the sensible approach.

In Freenity's example, there is no limitation to how many object levels you could have. Such as like:

php Code:
$this->pDb->pResult->pRow->name;

Oh the joys of OOP!

trs21219 04-27-2008 03:57 AM

Quote:

Originally Posted by Wildhoney (Post 13955)
Freenity is right. Well, not right, but it's also my most favourite way of implementation. Obviously there are occasions where inheritance is the sensible approach.

In Freenity's example, there is no limitation to how many object levels you could have. Such as like:

php Code:
$this->pDb->pResult->pRow->name;

Oh the joys of OOP!

lol thanks everyone i think i got it.


All times are GMT. The time now is 02:54 AM.

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