Jump to content

When do you use self:: as opposed to $this->?


linstefoo

Recommended Posts

This is a total newbie question but when do you use "self::" and when do you use "$this->" in a php class? They appear to do the same thing so is it just preference?

 

For example I have this code:

<?php
class Database
{
protected $info = array('server' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'apart_sql');
private $conn;
public $errors = array();

public function __construct()
{
	$this->connect(self::info['server'], self::info['usernamne'], self::info['password'], self::info['database']);
}
?>

 

Would I use "$this->" instead of "self::"? This is sort of a interesting to me.

 

Thanks,

Steve

Link to comment
https://forums.phpfreaks.com/topic/135470-when-do-you-use-self-as-opposed-to-this/
Share on other sites

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.