Poo - object class

Contenu du snippet

Une classe "racine" permettant d'éviter des erreurs fatales et facilitant le débogue, il suffit de l'étendre pour profiter de ces fonctionnalités.

Source / Exemple :


<?php
/**

  • Class Object is the root of the class hierarchy.
*
  • Every class has Object as a superclass and implements following features:
  • - Adds the ability to reverse-engineer classes, interfaces, functions,
  • methods and extensions of the object using PHP reflection API;
  • - Generates an unique identifier of an object that can be used as a hash key
  • for storing objects or for identifying an object;
  • - Prints a representation of the object when it is converted to a string;
  • - Avoids fatal errors (throws LogicException or BadMethodCallException
  • instead) when:
  • - writing or reading data from inaccessible properties;
  • - calling isset(), empty() or unset() on inaccessible properties;
  • - calling an object as a function;
  • - calling inaccessible methods in an object or static context.
* abstract class Object { /**
  • Returns an instance of ReflectionObject.
*
  • The ReflectionObject class is a part of the PHP Reflection API that adds
  • the ability to reverse-engineer classes, interfaces, functions, methods
  • and extensions of an object.
* *
  • @param void
  • @return ReflectionObject Returns an instance of ReflectionObject.
  • /
final public function getReflection() { static $reflection = null; return $reflection ?: $reflection = new ReflectionObject($this); } /**
  • Returns an unique identifier of the object.
*
  • This identifier is unique for each object and is always the same for the
  • same object. It can be used as a key for storing objects or for
  • identifying an object.
* *
  • @param void
  • @return string Returns an unique identifier of the object.
  • /
final public function getHashCode() { return spl_object_hash($this); } /**
  • Prints a representation of the object when it is converted to a string.
*
  • The string consisting of the name of the class of which the object is an
  • instance, the at-sign character `@`, and the unique identifier for the
  • object.
*
  • @param void
  • @return string Returns a string representation of the object.
  • /
public function __toString() { return $this->getReflection()->getName() . '@' . $this->getHashCode(); } /**
  • Throws an LogicException when writing data to inaccessible properties.
*
  • @param string $propertyName The name of the property being set.
  • @param mixed $propertyValue The value for the property.
  • @return void
  • @throws LogicException If one tries to write data to inaccessible
  • properties.
  • /
public function __set($propertyName, $propertyValue) { $className = $this->getReflection()->getName(); throw new LogicException("Write data to inaccessible property `{$className}::{$propertyName}`."); } /**
  • Throws an LogicException when reading data from inaccessible properties.
*
  • @param string $propertyName The name of the property to retrieve.
  • @return void
  • @throws LogicException If one tries to read data from inaccessible
  • properties.
  • /
public function __get($propertyName) { $className = $this->getReflection()->getName(); throw new LogicException("Read data from inaccessible property `{$className}::{$propertyName}`."); } /**
  • Throws an LogicException when calling isset() or empty() on inaccessible
  • properties.
*
  • @param string $propertyName The name of the property to be checked.
  • @return void
  • @throws LogicException If one tries to apply isset() or empty() on
  • inaccessible property.
  • /
public function __isset($propertyName) { $className = $this->getReflection()->getName(); throw new LogicException("Apply isset() or empty() on inaccessible property `{$className}::{$propertyName}`."); } /**
  • Throws an LogicException when calling unset() on inaccessible properties.
*
  • @param string $propertyName The name of the property to be unset.
  • @return void
  • @throws LogicException If one tries to apply unset() on inaccessible
  • property.
  • /
public function __unset($propertyName) { $className = $this->getReflection()->getName(); throw new LogicException("Apply unset() on inaccessible property `{$className}::{$propertyName}`."); } /**
  • Avoids a fatal error when calling an object as a function, throwing a
  • LogicException instead.
*
  • @param void
  • @return void
  • @throws LogicException If one tries to call an object as a function.
  • /
public function __invoke() { $className = $this->getReflection()->getName(); throw new LogicException("Call object `{$className}` as a function."); } /**
  • Avoids a fatal error when calling inaccessible methods in an object
  • context, throwing a BadMethodCallException instead.
*
  • @param string $methodName The name of the method being called.
  • @param array $arguments Optional, an enumerated array containing the
  • arguments passed to the method.
  • @return void
  • @throws BadMethodCallException If one tries to call inaccessible methods
  • in an object context.
  • /
public function __call($methodName, array $arguments = null) { $className = $this->getReflection()->getName(); throw new BadMethodCallException("Call to inaccessible method `{$className}::{$methodName}()`."); } /**
  • Avoids a fatal error when calling inaccessible methods in a static
  • context, throwing a BadMethodCallException instead.
*
  • @param string $methodName The name of the method being called.
  • @param array $arguments Optional, an enumerated array containing the
  • arguments passed to the method.
  • @return void
  • @throws BadMethodCallException If one tries to call inaccessible methods
  • in a static context.
  • /
static public function __callStatic($methodName, array $arguments = null) { $className = get_called_class(); throw new BadMethodCallException("Call to inaccessible static method `{$className}::{$methodName}()`."); } } // Example class Foo extends Object { const CONST_VALUE = 'A constant value'; private $_var = null; public function bar() { echo 'Hello World !'; } } try { $foo = new Foo(); // pritns: Array ( [CONST_VALUE] => A constant value ) // print_r($foo->getReflection()->getConstants()); // prints: 0000000050ce9726000000002e61f3e9 // echo $foo->getHashCode(); // prints: Foo@0000000050ce9726000000002e61f3e9 // echo $foo; // prints (LogicException): Write data to inaccessible property `Foo::_var`. // $foo->_var = 'Some value'; // prints (LogicException): Call object `Foo` as a function. // $foo('Hello World !'); // prints (BadMethodCallException): Call to inaccessible method `Foo::method(). // $foo->method(); // prints (BadMethodCallException): Call to inaccessible static method // `Foo::method()`. // $foo::method(); // prints: Hello World ! // $foo->bar(); } catch (Exception $exception) { echo $exception->getMessage() . PHP_EOL; } ?>

A voir également