Jump to content

Importing Classes


umbrella_thing

Recommended Posts

Hi Dudes and Dudets,

I have a questiong relating to PHP5 classes, I want to be able to use the methods and members of a class, inside another class!

For instance, a class that access the database. I would want to use this inside a perhaps an article class...

I know you can "include" a class and do Name::method(), but I'm not sure this is the best way...

is this possbile...
[code]
private dbConnection = new DataAccess

public function getArticlesById($id) {

$result = dbConnection->query("select * from article where id = $id");

}
[/code]

I don't know if this makes sense to anyone?


Link to comment
Share on other sites

Accessing members and methods of classes using the Scope Resolution Operator ( :: ) is not a bad way, when you're referencing a parent or static member.  The best way is through a form of polymorphism.  For instance, if I were to want access to the MySQLi class (build into PHP http://us3.php.net/mysqli), I could do so like this:

[code=php:0]
<?php
$mysqli = new mysqli('host','user','pass','db');

class myclass {
    private $db;
    public function __construct($db) {
          $this->db = $db;
    }
    public function __destruct() {
          $this->db->close();
    }
}
$test = new myclass($mysqli);
?>
[/code]

This will give you access within any class.  Another way would be this:

[code=php:0]
<?php

class myclass {

    private $db;
    public function __destruct() {
          $this->db->close();
    }
    public function __construct() {
          $this->db = new mysqli('host','user','pass','db');
    }
}
?>
[/code]

In the above example, the external class is actually instantiated within the class.  The final way would be to acually extend the class.
Link to comment
Share on other sites

[code]
<?php

abstract class mysql_database {
   
    public $connection;
   
    public function mysql_database($host="localhost", $user="", $pass="", $db="") {
        $this->connection = mysql_connect($host, $user, $pass);
       
        if($db != "") {
            $this->select_db($db, $this->connection);
        }
       
    }
   
    public function select_db($db) {
        if($db != "") {
            mysql_select_db($db, $this->connection);
        } else {
            die ("please select a database.");
        }   
    }
   
    public function query($q,$mode=MYSQL_ASSOC) {
        $retVal = array();
        $result = @mysql_query($q) or die("Invalid query: $q");
        while($row = @mysql_fetch_array($result,$mode)) {
            array_push($retVal, $row);
        }
        return $retVal;
    }
   
    public function queryCount($q) {
        $result = $this->query($q);
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
        return $row[0];   
    }
}


?>
[/code]

Thats my mysql class.

If you wanted to add functionality to it, you would:
[code]
<?php
class DataAccess extends mysql_database


}
?>
[/code]

You can use the BASE class methods in the new class, etc.

Good luck, let me know if you need any more help
- Keeb
Link to comment
Share on other sites

keeB,

Any reason you don't like MySQLi?  I see you are using abstract classes, so PHP5 is your version.  I'm sure if you're running PHP5 and have MySQL extensions, MySQLi is very likely available.

Unless you are currently using persistant connections, I would look into the MySQLi extension.

Also, the danger of using an abstract class for your DB mechanism is that you can't instantiate it.  It must be extended.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.