Jump to content

Recommended Posts

Hello people.

I'm new to OO PHP and started with writing my first class that creates MySQL connection. I would like someone to look at it and check if I'm following best OO practices. In particular I'm wondering if it's ok to declare a variable in a method when this variable wasn't declared previously in the beginning of the class e.g. $numRow, $dbArray, $rows

 

class mysql_query{

 

protected $_query;

protected $_result;

protected $_connect;

 

private $_root = "root";

private $_pass = "pass";

private $_host = "localhost";

private $_dbname = "database";

 

public function __construct($q) {

$this->_query = $q;

$this->_connect = mysqli_connect($this->_host, $this->_root, $this->_pass, $this->_dbname);

 

}

 

public function getResult() {

$this->_result = mysqli_query($this->_connect, $this->_query) or trigger_error("Query: $this->_query\n<br />MySQL Error: " . mysqli_error());

return $this->_result;

mysqli_close($this->_connect); // close connection

}

 

public function fetchNum() {

$numRow = mysqli_num_rows($this->_result);

return $numRow;

}

public function fetchArray() {

$dbArray = mysqli_fetch_array($this->_result);

return $dbArray;

}

public function fetchRow() {

$rows = mysqli_fetch_row($this->_result);

return $rows;

}

}

 

$query "some mysql query statement";

$db = new mysql_query($query);

$db->getResult();

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/186809-need-help-with-simple-oo-php-class/
Share on other sites

Calling a class mysql_query is also a very bad idea.

 

In particular I'm wondering if it's ok to declare a variable in a method when this variable wasn't declared previously in the beginning of the class e.g. $numRow, $dbArray, $rows

 

These are local variables in your  methods, not members of your class. So yeah, that's ok.

Calling a class mysql_query is also a very bad idea.

 

In particular I'm wondering if it's ok to declare a variable in a method when this variable wasn't declared previously in the beginning of the class e.g. $numRow, $dbArray, $rows

 

These are local variables in your  methods, not members of your class. So yeah, that's ok.

 

Thanks Mchl

 

Hmmm, Is there some vulnerability issue by calling a class the same name as a built in function?

 

Thanks

A query should a function within a database class, not a class it's self. Naming conversions are just as important coding logic. What I mean... If I see someone name a database class dbQuery or some other name that should purely only be a function within database class I tend to know that the coding logic will not be very sound. Sure everyone has their own coding style, but naming classes with names that will only confuse someone is simply illogical. A QUERY, QUERY_ERROR, etc, are logically member functions of a database class, not classes their self!

A query should a function within a database class, not a class it's self. Naming conversions are just as important coding logic. What I mean... If I see someone name a database class dbQuery or some other name that should purely only be a function within database class I tend to know that the coding logic will not be very sound. Sure everyone has their own coding style, but naming classes with names that will only confuse someone is simply illogical. A QUERY, QUERY_ERROR, etc, are logically member functions of a database class, not classes their self!

 

Thanks, that makes sense...

And it creates confusion. mysql_query is very common PHP function. While you, as the creator of the class will know what's going on, someone else might be surprised to see:

$foo = new mysql_query($sql);

 

Then they come here, post this piece of code, and ask US what the hell is going on, and we tell them, that mysql_query is a function, not a class, so they shouldn't try to instantiate it, but then they get error because there's no ext\mysql connection established etc., etc. :P

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.