Jump to content

Need some help with PHP Object code !!


ch1326

Recommended Posts

Hi, everyone. I'm having a problem with getting the result from my php code.

 

<?php
class Mysql{

	private $host;
	private $user;
	private $pass;
	private $db;

	public function __construct(){
		$this->host = "localhost";
		$this->user = "root";
		$this->pass = "password";
		$this->db = "person";
		$this->conn();
	}

	private function conn(){
		$conn = mysql_connect($this->host, $this->user, $this->pass);
		if(!$conn){
			mysql_error();
		}else{
			mysql_select_db($this->db, $conn);
		}
	}

	public function query($sql){
		$result = mysql_query($sql);
		return $result;
	}

	public function fetch_array($result){
		return mysql_fetch_array($result);
	}

	public function num_rows($result){
		return mysql_num_rows($result);
	}

}

$db = new Mysql();

/******************************************************************************************/




class Person{
	public $id;
	public $name;

	public function show_all(){
		return $this->by_sql("SELECT * FROM USERS");
	}

	public function show_by_id($id){
		return $this->by_sql("SELECT * FROM USERS WHERE id = {$id} LIMIT 1");
	}

	public function show_by_name($n){
		return $this->by_sql("SELECT * FROM USERS WHERE name = {$n}");
	}

	private function by_sql($sql){
		global $db;
		$result = $db->query($sql);
		$arr = array();
		while ($r = $db->fetch_array($result)){
			$arr[] = $this->initiate($r);
		}
		return $arr;
	}

	private function initiate($r){
		$obj = new self;
		foreach ($r as $att=>$val){
			$obj->$att = $val;
		}
		return $obj;
	}
}
?>

 

 

 

 

<?php
//phpinfo();
require_once("obj.php");

$user = new Person();
echo $user->show_by_id(1);
?>

Link to comment
https://forums.phpfreaks.com/topic/234327-need-some-help-with-php-object-code/
Share on other sites

Wow... okay, this is going to be long, so bear with me.

 

First, never, ever, ever use 'global'.  EVER.  This goes for both procedural programming and OOP, but especially with OOP.  'global' is the exact opposite of what OOP is all about, as it ties your objects to the environment in which they are created, nullifying their modularity and breaking their encapsulation.  Regardless of how you program, you should always have clean, clear, and explicit function/method signatures.  'global' is not explicit.  In fact, it creates an unknown (to the rest of the system) implicit requirement.  Simply put, if a function/method requires a parameter in order to work, pass that parameter through the argument list.  That's why it's there

 

Second, part of the 'magic' with OOP is that objects can contain permanent references to other objects.  This is known as composition, and is what you should be using here.  Here's how you do it:

 

class Mysql
{
   // class definition
}

class Person
{
   private $db;

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

   // rest of the definition, which uses $this->db to do work
}

$db = new Mysql(/* connection args */);
$person = new Person($db);

echo $person->show_by_id(1);

 

Third, your Mysql errors are never output to the screen.  You still need to echo or print them.

 

Finally, there's already an OO variant of MySQL in PHP - the MySQLi extension.  There's also an even more abstract, database agnostic option called PDO.  Chances are, you already have at least one of these, if not both, available to you.  Unless you're doing this for your own learning, I suggest using one of them.  mysqli pdo

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.