Skip to content

Instantly share code, notes, and snippets.

View mavik's full-sized avatar

Vitaliy Marenkov mavik

View GitHub Profile
@mavik
mavik / php_factory_debug_reflection.php
Last active March 20, 2022 13:32
Forbidding of creating objects outside factory in PHP with Reflection
<?php
class ClassA
{
public const FRIEND_CLASSES = [Factory::class];
protected function __construct() {}
}
trait Constructor
@mavik
mavik / php_factory_debug_backtrace.php
Last active March 20, 2022 13:18
Forbidding of creating objects outside factory in PHP with debug_backtrace
<?php
trait FactoryChecking
{
protected function checkFactory(string $factoryClass): void
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach($trace as $traceItem) {
if ($traceItem['class'] == $factoryClass) {
return;
### Keybase proof
I hereby claim:
* I am mavik on github.
* I am mavik (https://keybase.io/mavik) on keybase.
* I have a public key ASDX-BNtVNDLkoDJBE-sRVWKcuC2flyLfjwamT2WMPl4jgo
To claim this, I am signing this object:
### Keybase proof
I hereby claim:
* I am marenkov on github.
* I am mavik (https://keybase.io/mavik) on keybase.
* I have a public key ASBfWOFdpkFEZ8g9Rm8NRu-jbyyV0lfoLV3vYxMJz5_E7Ao
To claim this, I am signing this object: