简体   繁体   中英

PHP use parent class functions

I have Afactory mysql simple class and I can use with this method:

Afactory.class.php :

require_once ( substr( dirname(__FILE__), 0, -5 ).'config.inc' );
class AFactory
{
    private $DBhost;
    private $DBuser;
    private $DBpass;
    private $DBname;
    private $DBport;
    private $queryResult;
    private $linkConnection;
    public function __construct() 
    {
        $this->DBhost=LOCALHOST;
        $this->DBuser=USERNAME;
        $this->DBpass=PASSWORD;
        $this->DBname=DATABASE;
    }
    public function getDBO()
    {
        $linkConnection = mysql_connect ($this->DBhost , $this->DBuser , $this->DBpass);
        mysql_query("set charset set utf8", $linkConnection);
        mysql_query("set names 'utf8'", $linkConnection);           
        return  mysql_select_db($this->DBname) ? TRUE : FALSE;
    }
    public function setQuery($query)
    {
        return mysql_query($query);
    }
    public function loadAssoc()
    {
        $array=NULL;
        while($result = mysql_fetch_assoc($this->queryResult ))
             $array[] = $result;
        return $array;      
    }
}

with this file i can use class and class's functions

text.php:

$db=new AFactory();
$link=$db->getDBO();
$db->loadAssoc($db->setQuery("SELECT * FROM users")); // this can return array

i want to create new class and use Afactory class

testclass.php:

include ( substr( dirname(__FILE__), 0, -5 ).'alachiq_settings.php' );
class alachiq extends AFactory{
    public function __construct() {
       parent::__construct();
    } 
    public function fetchArray(){
       echo parent::getDBO(); //this line can return successfull connect to db
       return parent::loadAssoc(parrent::setQuery("SELECT * FROM users")); //fetch sql command

    }
}

after create alachiq class with Afactory extended i cant use this method:

useTestClass:

include('testclass.php');
print_r(  alachiq::fetchArray() );

What's my code problem?

First a short note on mysql:

Please, don't use mysql_* functions in new code . They are no longer maintained and are officially deprecated . See the red box ? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial .


Turn on your error_reporting / display_errors...

It's only a typo here ( parrent instead of parent ):

return parent::loadAssoc(parrent::setQuery("SELECT * FROM settings")); //fetch sql command
// -------------------------^

correct :

return parent::loadAssoc(parent::setQuery("SELECT * FROM settings")); //fetch sql command

Also, you cannot call non-static functions in a static context:

print_r(  alachiq::fetchArray() );

This was a static call; use a class instance:

$instance = new alachiq();
print_r(  $instance->fetchArray() );

The main problem you have here is a confusion of how to use static and object methods.

You are calling it like this:

print_r(alachiq::fetchArray());

That implies that you want to call it as a static method. The fetchArray method isn't declared as static , but you can get away with that if the method you're calling acts as a static method.

The problem, however comes when fetchArray calls parent::getDBO . getDBO makes reference to $this , which means that the method is not static .

This is why calling alachiq::fetchArray() fails; you're trying to make a static call on a non static method.

The solution:

You need to create an instance of the object and call that:

$myobj = new alachiq();
print_r($myobj->fetchArray());

That should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM