class in a class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FrEaKmAn
    New Member
    • Aug 2007
    • 46

    #1

    class in a class

    Hello

    I have a mysql_connectio n class, I think I don't need to post it here, inside it has a __construct() for connecting to db and basically everything works.

    What I want is to use - I have a variable $db = new mysql_db() - this variable in other classes. Right now I'm using the method that I declare class again, but then I have problem with getting data, because I'm getting errors like:

    Code:
    mysql_query(): supplied argument is not a valid MySQL-Link res...
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    Originally posted by FrEaKmAn
    Hello

    I have a mysql_connectio n class, I think I don't need to post it here, inside it has a __construct() for connecting to db and basically everything works.

    What I want is to use - I have a variable $db = new mysql_db() - this variable in other classes. Right now I'm using the method that I declare class again, but then I have problem with getting data, because I'm getting errors like:

    Code:
    mysql_query(): supplied argument is not a valid MySQL-Link res...
    Hi ,

    This is possible - I do it al the time - having asked this very question right here on TheScripts.

    Take for example a class called dataobject. this has been instantiaed and you have a local variable called $loDataObject (for arguments sake). Then you have a second object, secondObject (couldn't think of anything else) that you would like to be able to access $loDataObject.

    The plan is to instantiate the datObject first, then as you instantiate second object pass the $loDataobject to it:
    [php]
    // instantiate dataObject
    $loDataObject = new dataObject() ;
    // instantiate second object
    $loSecondObject = new secondObject($l oDataObject)
    [/php]
    Then in the definition of second object:
    [php]
    class secondObject
    var $loDBObject ;
    public function secondObject($p oDataObject)
    {
    $this->loDBObject->$poDB ;
    }
    }
    [/php]


    This works a treat in php 5 and I think it will work in php 4 if the function is not declared as public.

    Now in any function on $loSecondObject you can call the datobject via $this->loDBObect-> complete the blank with a property or a function and all is well.

    I hope this helps you out.

    Cheers
    nathj

    Comment

    Working...