-1

In a table class I want to use simple functions but also static functions, how can I do? Here is my current code (which does not work)

In my controller, I just want to do: Table::get('posts') which directly invokes the function check_table($table).

<?php
namespace Fwk\ORM;
use Fwk\Application;
use Fwk\Database\Database;

class Table extends Application {

    public function __construct()
    {
        $this->db = new Database();
    }

    public static function get($table) {
        if($this->check_table($table)) {
            return "ok";
        }

    }
    public function check_table($table) {
        $r = $this->$db->query("SELECT 1 FROM $table");
        return $r;
    }

}
?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Darkh
  • 19
  • 8
  • Do you actually know **why** you need the static methods? – B001ᛦ Jan 13 '17 at 15:06
  • At the moment the only function I want that is static is get, but other would be added in the next few days. – Darkh Jan 13 '17 at 15:09
  • This would be a really bad code design mixing up static and non static also you actually never need/should not use static methods as they are not testable. And my question before was **why** do you need static methods. You should ask yourself! – B001ᛦ Jan 13 '17 at 15:10
  • i think you cannot use this in a static function, because there is just one for all the instances of the class, and then not linked with an instance – Kaddath Jan 13 '17 at 15:11
  • Ok, so in my case it would be better to have a file with all the static method and another with simple functions? – Darkh Jan 13 '17 at 15:12
  • @bub `not use static methods as they are not testable` what an generic sentence. As long you can do everthing you want with php (and many dev does that), it is possible to test static methods, maybe not with phpunit. And `why do you need static`: Every Framework does in a way using one or more static methods in the core, see for example:Symfony http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html#method_create – JustOnUnderMillions Jan 13 '17 at 15:48
  • Downvotes for this question are not OK! Downvoter should always help to make the Q better. But i cant read a comment here that helps the OP to make the Q better. – JustOnUnderMillions Jan 13 '17 at 16:00

2 Answers2

0

You have to understand exactly what static means. When you declare a method as static, you're essentially saying "This method can be called directly without actually instantiating it's class". So while you're in a static method, you won't have access to $this since you're not in object context.

You could make check_table() static also and use it as a sort of factory:

public static function get($table) {
    if(self::check_table($table)) {
        return "ok";
    }

}
public static function check_table($table) {
    $r = (new Database())->query("SELECT 1 FROM $table");
    return $r;
}

http://php.net/manual/en/language.oop5.static.php

Chris Sprague
  • 368
  • 1
  • 4
  • 12
-1

You can try to put "self::MethodeName" instead of "this->MethodeName"