naike Posted October 18, 2010 Share Posted October 18, 2010 Hey, I'm trying to create an object oriented webpage, with just some basic features, like creating a user, logging in, admin restricted pages. However I'm stuck at this point. This is my code: class_user.php <?php include_once $_SERVER['DOCUMENT_ROOT'] . "/includes/class_database.php"; ?> <?php class User { private $database; public function __construct(MySqlDatabase $database) { //Type Hinting $this->database = $database; } public function find_all() { $result = $this->database->db_query("SELECT * FROM users"); return $result; } public function find_by_id($id=1) { $result = $this->database->db_query("SELECT * FROM users WHERE id={$id}"); $final = mysqli_fetch_array($result); return $final; } } ?> class_database.php <?php include_once $_SERVER['DOCUMENT_ROOT'] . "/includes/values.php"; include_once $_SERVER['DOCUMENT_ROOT'] . "/includes/class_user.php"; ?> <?php class MySqlDatabase extends MySQLi { function __construct() { //Check if constants are missing if (defined(!DB_USERNAME) || defined(!DB_SERVER) || defined(!DB_PASSWORD) || defined(!DB_NAME)) { die("One or more of the database constants are missing: " . mysqli_errno); } //Establish connection if constants are present using the parent class parent::__construct(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME); //Echo error message if connection has failed if ($this->connect_errno) { die("Database connection has failed: " . $this->connect_errno); } } public function db_query($sql) { $result = mysqli_query($this->connection, $sql); if (!$result) { die("Database query failed: " . $this->errno); } return $result; } public function query_prep($value) { $result = mysqli_real_escape_string($this->connection, $value); if (!$result) { die("Preparing query failed: " . $this->errno); } return $result; } } ?> Calling externally: <?php $database = new MySqlDatabase(); $user = new User($database); $found = $user->find_all(); ?> This is the error I'm getting: Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\includes\class_database.php on line 28 Database query failed: 0 I've tried everything I can come up with, but still I'm stuck at this error. Quote Link to comment https://forums.phpfreaks.com/topic/216184-object-class-confusion-unexplainable-error/ Share on other sites More sharing options...
ignace Posted October 18, 2010 Share Posted October 18, 2010 defined(!DB_USERNAME) is the same as defined(0) I think you mean !defined('DB_USERNAME') (mind the ') $result = $this->database->db_query("SELECT * FROM users WHERE id={$id}"); $final = mysqli_fetch_array($result); You either fully abstract your database or you don't, not trying to use both or you'll still end up screwed. The same goes for: __construct(MySqlDatabase $database) In OO we have a rule: program to an interface, not an implementation. Thus something like: interface DatabaseInterface { function connect($user, $pass, $host = 'localhost'); function disconnect(); function isConnected(); function prepare($sql); function execute($sql = null, $params = array()); function escape($value); } class MySQLDatabase implements DatabaseInterface { // implement the above methods } class UserGateway { private $db = null; function __construct(DatabaseInterface $db) { $this->db = $db; } function findById($id) { return new User( $this->db->select()->from('users')->where('id', $id); ); } function fetchAll($count = null, $offset = null) { if($count != null && $offset != null) { return new UserList( $this->db->select()->from('users')->limit($count, $offset); ); } else if($count != null) { return new UserList( $this->db->select()->from('users')->limit($count); ); } else { return new UserList( $this->db->select()->from('users'); ); } } } Quote Link to comment https://forums.phpfreaks.com/topic/216184-object-class-confusion-unexplainable-error/#findComment-1123561 Share on other sites More sharing options...
naike Posted October 18, 2010 Author Share Posted October 18, 2010 defined(!DB_USERNAME) is the same as defined(0) I think you mean !defined('DB_USERNAME') (mind the ') $result = $this->database->db_query("SELECT * FROM users WHERE id={$id}"); $final = mysqli_fetch_array($result); You either fully abstract your database or you don't, not trying to use both or you'll still end up screwed. The same goes for: __construct(MySqlDatabase $database) In OO we have a rule: program to an interface, not an implementation. Thus something like: interface DatabaseInterface { function connect($user, $pass, $host = 'localhost'); function disconnect(); function isConnected(); function prepare($sql); function execute($sql = null, $params = array()); function escape($value); } class MySQLDatabase implements DatabaseInterface { // implement the above methods } class UserGateway { private $db = null; function __construct(DatabaseInterface $db) { $this->db = $db; } function findById($id) { return new User( $this->db->select()->from('users')->where('id', $id); ); } function fetchAll($count = null, $offset = null) { if($count != null && $offset != null) { return new UserList( $this->db->select()->from('users')->limit($count, $offset); ); } else if($count != null) { return new UserList( $this->db->select()->from('users')->limit($count); ); } else { return new UserList( $this->db->select()->from('users'); ); } } } So I should completely drop my style of coding right away? Do you have any (dymmy friendly) websites that explain this implementation procedure? Also, where do those very above functions come from, you aren't defining them, or was it just an example? Quote Link to comment https://forums.phpfreaks.com/topic/216184-object-class-confusion-unexplainable-error/#findComment-1123603 Share on other sites More sharing options...
ignace Posted October 19, 2010 Share Posted October 19, 2010 Yes these are mere examples. I'm not the one they hired to write the code for them. Quote Link to comment https://forums.phpfreaks.com/topic/216184-object-class-confusion-unexplainable-error/#findComment-1123749 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.