1

I have a class named "dbConnect" and another one for uploading files "uploadFiles", and I would like to call a function from "dbConnect" inside "uploadFiles":

dbConnect:

final class dbConnect {
// some codes and functions


public final function prepareStm($query){

        $this->query = $query;

        $insertDB = $connexion->prepare($this->query);          
} // end prepareStm()

// some other codes and functions

}

Now in uploadFiles:

final class uploadFiles {
// some codes and functions


public final function scanFile() {

    $time_start = microtime(true);

    cl_engine(10000, 734003200, 734003200, 25, 0);

    ini_set('max_execution_time', 120);

    $retcode = cl_scanfile($this->fileTmp, $virusname);

    ini_restore('max_execution_time');

    $time_end = microtime(true);
    $timeTaken = $time_end - $time_start;

    $this->timeTaken    = $timeTaken;

        if ($retcode == CL_VIRUS) {
            return true;
                $this->aErrors[] = "File path : ".$this->fileTmp." Return code : ".cl_pretcode($retcode)." Virus found name : ".$virusname; 



// AND HERE I would like to call the "prepareStm" function from the dbConnect class to insert some information inside the database

} 

            return false;


} // end scanFile

// some other codes and functions

}

EDIT: This is a simple one:

<?php

require_once 'classA.php';

final class classB {

 public $prop1 = "I'm a class property!";

    public function setProperty($newval)
    {
        $this->prop1 = $newval;
    }

    public function addProperty()
    {   
        $connect = new classA();
        $connect->getProperty();
        $res = $connect->getProperty();
    }
}

$obj = new classB;

echo $obj->addProperty();

?>

How can I call a function named "functionA" from class named "classA"?

Thanks for your support

Oum Alaa
  • 227
  • 3
  • 11
  • What's wrong with `$this->dbConnect()`? – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 02 '16 at 03:42
  • I did not try it, I don't know how to call a function from another class – Oum Alaa Jan 02 '16 at 03:48
  • Sorry, I thought it was a function in the same class. It's not. Pls check the answer below. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Jan 02 '16 at 04:04
  • 1
    @Oum Alaa, you likely want to use a singleton for the dbConnection, and build it as a static class. See [this answer](http://stackoverflow.com/questions/18885421/trying-to-build-a-static-database-class-that-i-can-access-from-any-function-outs) for details. You could also check out [MeekroDB library](http://meekro.com/) for simple, lightweight database connections. It protects from SQL injection, too. – Ville Jan 02 '16 at 04:18
  • I am using PDO with prepare and execute, in between bindparams as well – Oum Alaa Jan 02 '16 at 04:59

2 Answers2

1

Maybe you can write like this:

$connect = new dbConnect();
$connect->prepareStm($query);
sis
  • 68
  • 9
1

There are two ways to invoke a function from a separate class.

The first is to instance the class as an object and have that object call the function.

$db = new dbConnect();
$db->prepareStmt($sql);

If you need to get the results from a return statement, just have a variable equal to it.

$res = $db->prepareStmt($sql);

The second method is to call the function statically from the class itself.

dbConnect::prepareStmt($sql);

Either method will need you to include the class as well.

include dbConnect.php;
hugmungus
  • 876
  • 1
  • 13
  • 23
  • I updated my question above with a simple test, it that right? – Oum Alaa Jan 02 '16 at 04:39
  • You don't need to make class B an object unless you're going to run class B from another file (hopefully a controller), but what you have in the addProperty function looks correct. Also, you need to include class A into class B, otherwise you'll get an undefined method error. How are you planning to run this code? Sounds like it would be worth your time to read up on object oriented programming. – hugmungus Jan 02 '16 at 04:46
  • I have each class in seperated files – Oum Alaa Jan 02 '16 at 04:52
  • Great job, happy I could help – hugmungus Jan 02 '16 at 04:59