Title: PHP object is null after initialization
Tags: php;class;object
Question: I have the class file:

```class Settings
{
public $siteName;
public $siteAddr;
public $dbUrl;
public $dbName;
public $dbPass;

public function __construct()
{
$siteName = 'Welcome';
$siteAddr = 'http://site.com';
$dbUrl = 'test';
$dbName = 'base';
$dbPass = '123';
}
}
```

Trying to use it in other file:

```require_once('settings.php');
$cfg = new Settings();
var_dump($cfg); // <--- everywhere is null...
```

Why does it contain only nulls?


Here is the accepted answer: Instead of 

```$siteName = 'Welcome';
```

you need

```$this-&gt;siteName = 'Welcome';
```

otherwise you just created variables in scope of your constructor, not initialized object members.


Here is another answer: The variables you're using in your constructor are local scope variables. To refer to class fields use ```$this-&gt;fieldname```.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:08/21/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><class><object>', 'Title': 'PHP object is null after initialization', 'ViewCount': 100, 'id': 18367235, 'user_AboutMe': '', 'user_AccountId': 2238102.0, 'user_DisplayName': 'FireForce', 'user_Reputation': 23.0, 'user_Views': 15.0, 'user_id': 1973869.0}}