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
https://forums.phpfreaks.com/topic/197856-need-help-with-oop-please/
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.

  Quote

Also:

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

 

  Quote
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

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.