SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    accessing a class from within a class

    how do I access a class from within a class?

    I'll give an example that wont work, but it's what I want to do



    PHP Code:
    class db{

    function 
    connect(){
    //connect to db
    }

    function 
    query(){ //query }

    ...

    }

    $Db = new db();
    $Db->connect();

    class 
    wut{

    function 
    whatever(){

    $Db->query($sql);

    }}

    $woot = new wut();
    $woot->wut(); 

    I've not tried it, but could I use db::query($sql) in the second class?
    but, wouldn't I have to connect to the database via db::connect again since it's static or w/e?

    edit
    would doing something like

    PHP Code:
    //in class two

    var $h;

    $h->query($sql);

    //instead of just $woot->wut();
    $woot->$Db;
    $woot->wut(); 
    work?

    my schools blocked my host, so I can't find out

  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)
    A class is not an object. A class is an abstract type. An object is a concrete instance of a class. You can't access a class -- you access an object.

    Try this:
    PHP Code:
    class DB
    {
      function 
    connect() {
        
    //connect to db
      
    }
      function 
    query() {
        
    //query
      
    }
    }

    class 
    Wut
    {
      var 
    $db;
      function 
    Wut($db) {
        
    $this->db $db;
      }

      function 
    whatever() {
        
    $this->db->query($sql);
      }
    }

    $db = new DB();
    $db->connect();

    $woot = new Wut($db);
    $woot->wut(); 

  3. #3
    Non-Member I87's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that worked
    thanks


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
  •