0
User::updatemain($set, $where); 

This gives Fatal error: Using $this when not in object context My user class extends from Dbase class and here is user class function:

public static function activate($set, $where) {
    return $this->updatemain($set, $where);

here is dbase class (some part of):

private function query($sql = null, $params = null) {
    if (!empty($sql)) {
        $this->_last_statement = $sql;
        if ($this->_db_object == null) {
            $this->connect();
        }
        try {
            $statement = $this->_db_object->prepare($sql, $this->_driver_options);
            $params = Helper::makeArray($params);
                $x = 1;
                if (count($params)) {
                    foreach ($params as $param) {
                        $statement->bindValue($x, $param);
                        $x++;
                    }
                }
            if (!$statement->execute() || $statement->errorCode() != '0000') {
                $error = $statement->errorInfo();
                throw new PDOException("Database error {$error[0]} : {$error[2]}, driver error code is {$error[1]}");
                exit;
            }
            //echo $sql;
            return $statement;
        } catch (PDOException $e) {
            echo $this->formatException($e);
            exit;
        }
    }
}

public function updatemain($set, $where) {
    return $this->query($sql, $params);
}

this is part of Dbase class

Newmaster
  • 57
  • 6

3 Answers3

2

You are calling static method so there is no $this in that context.

If you want to call other static method from given class then use self::method() but if you want to call non-static method you've got problem. First you have to create new object.

Elon Than
  • 9,603
  • 4
  • 27
  • 37
1

When you use static methods, you can't use $this inside

public static function activate($set, $where) {
       return self::updatemain($set, $where);
}

Or you have to use singelton design

donald123
  • 5,638
  • 3
  • 26
  • 23
  • updateMain uses `query()` which is totally instance method ... :) it will not work this way. The idea behind static methods is that they are stateless.. in the OP design the architecture has way too many dependencies from the object – Royal Bg Jan 29 '15 at 11:43
0

EDIT

Best solution - rewrite your class to one point access to DB object. And create Model classes to DB access. See my example code below:

core AppCore

<?php
class AppCore
{
    public static $config  = array();
    public static $ormInit = false;

    public static function init($config)
    {
        self::$config = array_merge(self::$config, $config);
    }

    public static function db($table)
    {
        // ORM - see http://idiorm.readthedocs.org/en/latest
        if (!self::$ormInit) {
            ORM::configure(self::$config['db']['connection']);
            ORM::configure('username', self::$config['db']['username']);
            ORM::configure('password', self::$config['db']['password']);
            self::$ormInit = true;
        }
        return ORM::for_table($table);
    }
}

User model

<?php
class UserModel
{
    const TABLE = 'user';

    public static function findById($u_id)
    {
        $result = AppCore::db(self::TABLE)->where('u_id', $u_id)->find_one();
        return $result ? $result->as_array() : null;
    }
}

AppCore init section

AppCore::init(array(
    'db' => array(
        'connection' => "mysql:dbname={$db};host={$host}",
        'username' => $user,
        'password' => $pass
    ),
));

i hope it help make your code better

stepozer
  • 1,143
  • 1
  • 10
  • 22
  • When the things get horrible... just make them more horrible? The OP needs to fix all his domain logic. It's totally broken :) – Royal Bg Jan 29 '15 at 11:46
  • @RoyalBg if i suggest use `ActiveRecord` and `singletone` it kill him:) – stepozer Jan 29 '15 at 11:48
  • Most probably :) Btw. there's one more thing that will not work in updateMain - wrong variable names. It recieves $set and $where, but sends $sql and $params – Royal Bg Jan 29 '15 at 11:50
  • @RoyalBg i don't really understand why people always write self DB class? A lot of frameworks and libraries exists now... For example http://idiorm.readthedocs.org/en/latest/ – stepozer Jan 29 '15 at 11:52
  • actually $params = $set, i tried to post whole both classes but i could not, i have singltone method in Dbase class – Newmaster Jan 29 '15 at 11:55
  • This actually isn't an implementation of the [singleton pattern](http://stackoverflow.com/a/203359/777850), it's just a class with only static methods and properties and a `init` method... – giorgio Jan 29 '15 at 12:42