Mhari Posted December 9, 2017 Share Posted December 9, 2017 Hi, I dont know what title that is more suitable for this topic but I will try to explain it as well as I can I have a class that I will use it for interacting with database I facing a problem when calling the method with "::" assign here is the code <?php class Crud { protected $data; protected $db; protected $query; protected static $action; protected static $instance = null; protected static $columns = []; protected $table; public function __construct() { $this->db = new mysqli('localhost', 'root', '', 'bahan_belajar'); if (!$this->db) { // throw new Exception("Koneksi error", $this->db->errno); echo "error"; } return $this; } public static function getInstance() { if (self::$instance === null) { self::$instance = new self; } return self::$instance; } public function select() { if (empty(func_get_args())) { // $this->columns = "*"; self::$columns = "*"; } else { if (is_array(func_get_args())) { // self::columns = join(', ', func_get_args()); self::$columns = join(', ', func_get_args()); } else { // self::columns = func_get_args(); self::$columns = func_get_args(); } } self::$action = "SELECT"; return $this; } public function from($tableName) { $this->table = ' FROM ' . $tableName; return $this; } public function get($getName = 'object') { $this->query = self::$action . ' ' . self::$columns . ' ' . $this->table; switch ($getName) { case 'object': $this->data = $this->db->query($this->query)->fetch_object(); break; case 'array': $this->data = $this->db->query($this->query)->fetch_array(); break; case 'count': $this->data = $this->db->query($this->query)->num_rows; break; } return $this->data; } } $chat = Crud::getInstance()->select('nama', 'teks')->from('chat')->get(); echo '<pre>'; print_r($chat); echo '</pre>'; actually this code was work fine when I put getInstance() method in the first method but how to make it work without putting that method? example : $chat = Crud::select('nama', 'teks')->from('chat')->get(); this will produce Fatal error like : Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\bahan_belajar\chat\classes.php:47 Stack trace: #0 C:\xampp\htdocs\bahan_belajar\chat\classes.php(74): Crud::select('nama', 'teks') #1 {main} thrown in C:\xampp\htdocs\bahan_belajar\chat\classes.php on line 47 I just want to call it with "::" assign and not with "->" assign in first the method please help me how to solve this also I am sorry for my bad code structures thanks in advance Link to comment Share on other sites More sharing options...
Don E Posted December 9, 2017 Share Posted December 9, 2017 Function select is not defined as a static method. PHP gave the error because select is not a static method and when trying to access a non static method, you need to create an object for it, for that class to access that method. When not, like in your code, you're using $this as a non object resulting in the fatal error. getInstance is a static function thus why it worked. Link to comment Share on other sites More sharing options...
Mhari Posted December 10, 2017 Author Share Posted December 10, 2017 Quote you need to create an object for it, for that class to access that method. can you please give me an example on how I applying this to my code? Link to comment Share on other sites More sharing options...
iwato Posted December 10, 2017 Share Posted December 10, 2017 (edited) You appear to have two choices: 1) Redefine your method as static -- namely, public static function select() {...} 2) Access your method via a Crud object. $crud_obj = new Crud(); $crud_obj->select(...); Edited December 10, 2017 by iwato Better clarity. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 11, 2017 Share Posted December 11, 2017 I just want to call it with "::" assign and not with "->" assign in first the method Why? Why is that important to you? There are reasons why the class is set up that way (even though it could be set up in a better way), so what reasons do you have for not wanting it that way? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now