1

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?

Alfergon
  • 5,463
  • 6
  • 36
  • 56
  • A tentative question: why use an abstract factory pattern for database connections? Do you connect to a lot of databases simultaneously? Not criticizing (yet :P ), just curious. – Maxim Kumpan Jun 11 '13 at 11:09

2 Answers2

7

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() );
Zoe
  • 27,060
  • 21
  • 118
  • 148
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • 1
    @mahdipishguy because you anywhere set `$this->queryResult`? You maybe want to set it in setQuery method instead of returning? – bwoebi Jun 11 '13 at 11:38
  • @mahdipishguy or because you don't accept a parameter in loadAssoc method, but use the not set property... – bwoebi Jun 11 '13 at 11:41
  • @mahdipishguy If this answer solved your problem, consider upvoting and accepting it, by clicking on the large green tick mark (✔) under the answer's score. – bwoebi Jun 11 '13 at 12:25
1

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.

bwoebi
  • 23,637
  • 5
  • 58
  • 79
Spudley
  • 166,037
  • 39
  • 233
  • 307