Page 2 of 2
Re: MySQL using OO PHP
Posted: Sat Aug 22, 2009 5:16 pm
by MichaelR
Darkhazer wrote:The difference between private and protected is that if you extends the class, the subclass (the extended class) will still have the property $connection (if the property is protected).
...
Edit: since the original code is modified, it's possible that my post was about public => protected, but now I would tell about private => protected
I've found that if I make $connection a protected member, and try to inherit it in the MySQLResult class, it returns as null, even if returning as a resource in MySQL. I can't work out why this is.
Re: MySQL using OO PHP
Posted: Mon Aug 24, 2009 7:44 am
by Jenk
Darhazer wrote:Actually my post was:
Change private $connection to protected $connection
And it have nothing to do with public properties
The difference between private and protected is that if you extends the class, the subclass (the extended class) will still have the property $connection (if the property is protected).
So if you have no good reason to declare something as private, do not do that
Otherwise you or someone else will extend the class, and will have hard time catching why the inherited methods do not work
Edit: since the original code is modified, it's possible that my post was about public => protected, but now I would tell about private => protected
but my question still stands unanswered.
Re: MySQL using OO PHP
Posted: Mon Aug 24, 2009 11:52 am
by Darhazer
I think we've answer both 'why not public' and 'why not private'.
Let me know if further clarification if needed
MichaelR
If you make a property private/protected - you cannot access it directly outside the class. You need to provide a getter for that property (or method that checks the property and returns true/false)
If you throw exceptions, you need to alter the error handling code as well, to catch the exceptions.
Re: MySQL using OO PHP
Posted: Mon Aug 24, 2009 11:58 pm
by MichaelR
This is an example of what I mean:
Code: Select all
<?php
class MySQL {
protected $connection;
public static function connect($host, $username, $password) {
return new MySQL($host, $username, $password);
}
private function __construct($host, $username, $password) {
$this->connection = mysql_connect($host, $username, $password);
}
public function return_connection() {
if ($this->connection) {
return $this->connection;
}
}
public function query($query) {
if ($this->connection) {
return new MySQLResult($query);
}
}
}
class MySQLResult extends MySQL {
private $result;
protected function __construct($query) {
$this->result = mysql_query($query, $this->return_connection());
}
}
$database = MySQL::connect('localhost', 'root', 'password');
$user = $database->query("SELECT `username` FROM `database`.`users` WHERE `id` = 1 LIMIT 1");
?>
This gives the error code: Warning: mysql_query() expects parameter 2 to be resource, null given in C:\Users\Michael\Websites\htdocs\MySQLClasses.php on line 38.
Edit: I'm an idiot. Please ignore.