Morten Hagh
Morten Hagh

Reputation: 2113

Using SQL function from SQL handling class in other functions

I have created an SQL function SQLquery in a class called SQLHandling It looks like this:

 /***********************************************************
    * SQLquery takes a full SQL query and runs it
    * If it fails it will return an error otherwise it will return
    * the SQL query as given.
    ************************************************************/   
    function SQLquery($query)  { 

        $q = mysql_query($query);

        if(!$q) {
            die(mysql_error());
            return false;
        } else {
            return $q;          
        }
    }

Is there anyway I can use this function in other classes functions without adding

$db = new SQLHandling();
$db->SQLquery($sql);

In every function where I will use it.

I know I can run SQLHandling::SQLquery($sql); but I am trying to avoid that.

Upvotes: 1

Views: 140

Answers (2)

Mithun Satheesh
Mithun Satheesh

Reputation: 27855

use inheritance

refer: http://php.net/manual/en/language.oop5.inheritance.php

but still you will need to use parent::fun() or $this->fun() or put it as a public function then use any where.

example:

<?php

function c()
{
        echo "moi";
}    

class b extends a
{       
   public function d(){

    parent::c();//Hai
    $this->c();//Hai
    c();//moi

    }   


}


class a{    



    public function c(){
        echo "Hai";

    }       
} 


$kk = new b();
$kk -> d();

?>

Upvotes: 2

Sherlock
Sherlock

Reputation: 7597

You could instantiate SQLHandling on class level, like so:

    private $db;

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

    public function x()
    {
        $this->db->query('X');
    }

Upvotes: 1

Related Questions