Jump to content

Defining $this->variable


9three

Recommended Posts

Hey,

 

if I do the following:

 

class mysql
{
  private $host, $username, $password, $dbname;
  
  public function __construct($host = '', $username = '', $password = '', $dbname = '')
  {
    $this->host = $host;
    $this->username = $username;
    $this->password = $password;
    $this->dbname = $dbname;
  }
  
  public function connect()
  {
    $this->link = mysql_connect($this->host, $this->username, $this->password);
    if (!is_resource($this->link))
      Throw New Exception(mysql_error());
  }
  
  public function select()
  {
    $this->select = mysql_select_db($this->dbname);
  }
}

 

If you notice, I'm not defining $this->link or $this->select. Is there a reason I should? Is this a security issue?

Link to comment
https://forums.phpfreaks.com/topic/144877-defining-this-variable/
Share on other sites

Hey, just more questions that are coming to mind now:

 

The more variables I declare at the beginning, the slower the application becomes?

 

I'm thinking of two ways to generically pull information from a database with fetch a Assoc and fetch as an Array.

 

public function fetchAssoc()
{
  $this->fetchAssoc = mysql_fetch_assoc($this->query, $this->link);
  if (!is_resource($this->fetchAssoc))
    Throw New Exception(mysql_error());
  else
  {
    $array = array();
    while ($field = $this->fetchAssoc)
    {
       $array[] = $field; 
    }
    return $array;
  }
}

 

Or would it be better if I just did something like this:

 

public function fetchAssoc()
{

  $array = array();
  while ($field = mysql_fetch_assoc($this->query, $this->link))
  {
     $array[] = $field; 
  }
  return $array;

}

 

What would be better for performance, and security? I know though that using the first method would actually benefit me in certain ways because of I'm error handling it properly.

 

What do you guys think?

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.