Jump to content

need help with OOP please


ch1326

Recommended Posts

Got some errors from this code. need help please

 

<?php
class MYSQL {
	var $host = "localhost";
	var $user = "root";
	var $pass = "password";
	var $db = "userlist";

	function __construct($host, $user, $pass, $db){
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->db = $db;
	}

	public function query($sql){
		$this->connDB();
		$result = mysql_query($sql);
		return fetchArray($result);
	}

	public  function fetchArray($result){
		while ($row = mysql_fetch_array($result) ){
			echo "Name is ". $row["name"];
		}
	}

	public function connDB () {
		$conn = mysql_connect($this->host, $this->user, $this->pass);
		mysql_select_db($this->db, $conn);
	}
}

$example = new MYSQL();
echo $example->query("select * from user");
?>

Link to comment
Share on other sites

Well, first, if you define your constructor to take arguments, you must actually pass arguments to it when you invoke it, unless you define those arguments as default arguments.  In other words, the following will throw an error:

 

class MYSQL 
{
   var $host = "localhost";
   var $user = "root";
   var $pass = "password";
   var $db = "userlist";
      
   function __construct($host, $user, $pass, $db)
   {
      $this->host = $host;
      $this->user = $user;
      $this->pass = $pass;
      $this->db = $db;
   }
}

$db = new MYSQL();

 

Whereas the following will not:

 

class MYSQL
{
   var $host = "localhost";
   var $user = "root";
   var $pass = "password";
   var $db = "userlist";
      
   function __construct($host = '', $user = '', $pass = '', $db = '')
   {
      $this->host = $host;
      $this->user = $user;
      $this->pass = $pass;
      $this->db = $db;
   }
}

$db = new MYSQL();

 

Note, however, that the constructor in this second example is pretty useless as written, as it will merely set all your data members to empty strings.

 

You should also use access modifiers for your data members.  The 'var' keyword works, but is essentially deprecated (it's a remnant of PHP 4), and makes those data members publicly accessible, which is pretty useless.  Data members should be protected or private, with appropriate getter and setter methods.

Link to comment
Share on other sites

Also:

1: 'var' should not be used in PHP5. Use proper visibility modifiers instead (public/protected/private)

 

You should also use access modifiers for your data members.  The 'var' keyword works, but is essentially deprecated (it's a remnant of PHP 4), and makes those data members publicly accessible, which is pretty useless.  Data members should be protected or private, with appropriate getter and setter methods.

 

:P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.