1

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.

2 Answers 2

2

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();

?>
Sign up to request clarification or add additional context in comments.

1 Comment

You are suggesting he should extend SQLHandling in other classes? I don't see any reason to do so..
1

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');
    }

1 Comment

This is exactly what I was looking for! Spot on! Thank you very much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.