Skip to content

Instantly share code, notes, and snippets.

View brzuchal's full-sized avatar

Michał Marcin Brzuchalski brzuchal

View GitHub Profile
@brzuchal
brzuchal / namespace.out
Last active October 28, 2016 11:33
Introducing namespace as struct in PHP
===========================================
Namespace: 'PHPTest'
===========================================
Array
(
[name] => PHPTest
[internal] =>
[user-defined] => 1
[parent] => ReflectionNamespace Object
(
@brzuchal
brzuchal / mutator.php
Last active September 7, 2016 13:08
Immutable classes - mutator function
<?php
final immutable class Currency {}
final immutable class Money {
public $amount;
public $currency;
public function __construct(float $amount, Currency $currency) {
$this->amount = $amount;
$this->currency = $currency;
}
@brzuchal
brzuchal / immutable.php
Last active August 9, 2016 10:38
PHP Immutable class/property
<?php
immutable class Currency
{
/** @var string */
public $code; // this will change to readonly after constructor execution
/** @var string */
public $name; // this will change to readonly after constructor execution
/** @var int */
public $fractionDigits;
@brzuchal
brzuchal / src\MyInternalStuff\myNonPublicWidget.php
Last active June 8, 2016 16:28
PHP package-private class members
<?php
package MyVendor\MyLibrary // acts same as `namespace` but changes all classes into package-private
{
namespace MyInternalStuff // cause current namespace would be MyVendor\MyLibrary\MyInternalStuff
{
class MyNonPublicWidget // class name would be MyVendor\MyLibrary\MyInternalStuff\MyNonPublicWidget
{
}
}
@brzuchal
brzuchal / articles.php
Created April 5, 2016 09:55
PHP Final Property redeclaration as non-final
<?php
class Article {
private final $comments = [];
public function addComment(Comment $comment) {
$this->comments[] = $comment;
}
public function clearComments() {
$this->comments = [];
}
}