SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $this->differclass->function() is it possible

    Hi

    I want to do the following

    PHP Code:
     
     
    class A
    {
    var 
    $db;
      function 
    __construct($dbclass)
    {
     
    $this->db =& $dbclass;
     
    $sql 'some query';
    $this->db->query($sql);
     
    }
     
    // etc //
     


    is that possible what am I missing?

    i've been gone from php for a while now some i'm getting the hang of it again but this is really puzzling me.

    Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. What is the question?

  3. #3
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks it means I'm not crazy

    PHP Code:
    function __construct($config,$db)
     {
     
      
    $this->Clear();
     
      if (
    $id == '')
       
    $this->Init();
      else
       
    $this->LoadId($id);
     
      
    $this->db =& $db
      if (isset(
    $this->db)) {
       echo 
    'it is not loaded ';
      }
      else 
      {
       echo 
    'it is loaded';
      }
     
     } 
    I keep getting it is not loaded.. I can use the $db everywhere else... I don't get this! how can I fix this or atleast find out what is happening.

    peanuts
    Last edited by peanuts; May 8, 2007 at 15:22.
    the neigbours (free) WIFI makes it just a little more fun

  4. #4
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Swap the text 'it is not loaded' and 'it is loaded'.

    Or change if(isset($this->db)) to if(!isset($this->db)).

  5. #5
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im just going to bed now and curl up crying of shame ...

    how obvious.. sorry to bother you and thanks!
    the neigbours (free) WIFI makes it just a little more fun

  6. #6
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    I'm still going crazy ..

    in the construct function it seems to be working..

    but if I create more functions and try to call $this->db it is not set.. and I get an error like this:

    Fatal error: Call to a member function on a non-object.

    does anybody have an idea?
    the neigbours (free) WIFI makes it just a little more fun

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by peanuts View Post
    Hi

    I want to do the following

    PHP Code:
     
     
    class A
    {
    var 
    $db;
      function 
    __construct($dbclass)
    {
     
    $this->db =& $dbclass;
     
    $sql 'some query';
    $this->db->query($sql);
     
    }
     
    // etc //
     


    is that possible what am I missing?

    i've been gone from php for a while now some i'm getting the hang of it again but this is really puzzling me.

    Peanuts
    Object is assigned by Reference in PHP 5, I don't think you need to use &, $this->db =& $dbclass; should be $this->db = $dbclass;

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...

    Still trying to understand what it is you want to do, but...

    PHP Code:
    // ...
    if ($id == '')
       
    $this->Init();
      else
       
    $this->LoadId($id);
    // ... 
    This should be encapsulated within it's own class method, for a number of reasons. I would instead, have something like this,

    PHP Code:
    public function __construct( ... ) {
    $this -> prepare$id );
    // ...
    }

    protected function 
    prepare$id false ) {
    $this -> clear();
    if( 
    $id ) {
    $this -> initialise(); // don't use abbrev. as it's not helpful
    } else {
    $this -> load$id );
    }
    }
    // ... 
    Once you have removed the responsibility out of the constructor, you are then able to have more flexibility, as you can put the prepare() method in the base class, and later if required, overwrite it with one in the concrete implementation.

    That point, and the fact that the constructor should have as little knowledge of the internal workings of the class as possible.

    > does anybody have an idea?

    Which version of PHP are you using? The constructor style suggests PHP5 though you are using a reference.

  9. #9
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys ..

    the reference is just a bad habit I supose .. I'll take them out..

    @dr. Livingstone.
    Good point .. I'll rewrite some of my classes for that..

    To be honest I've been out of the game for a while and find myself a lot more rusty than I would have imagetioned.

    Peanuts
    the neigbours (free) WIFI makes it just a little more fun


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •