Sidebars examples
Private_constructor.php
Code
<?php
class Account {
protected $type;
private function __construct($type)
{
$this->type = $type;
}
public static function open ($type)
{
return new static($type);
}
public function type()
{
return $this->type;
}
}
$accounts = [
Account::open('savings'),
Account::open('checking'),
Account::open('savings'),
Account::open('checking')
];
echo '<pre>';
var_dump(
array_filter($accounts, function ($account) {
return $account->type() == 'savings';
})
);
echo '</pre>';
?> Output
array(2) {
[0]=>
object(Account)#1 (1) {
["type":protected]=>
string(7) "savings"
}
[2]=>
object(Account)#3 (1) {
["type":protected]=>
string(7) "savings"
}
}