php-basics/.phpdoc/guide/overview/traits.rst

114 lines
4.1 KiB
ReStructuredText
Raw Normal View History

2024-03-26 20:10:30 +01:00
.. title:: Traits
Traits
######
.. sidebar:: Table of Contents
.. contents::
2024-03-31 17:41:04 +02:00
This package provides a number of generic traits like different getter and setter methods, an implementation of the
singleton design pattern and some little helpers. Those traits are too small to justify their own packages and most of them
are dependencies of the :doc:`datastructures` and :doc:`interfaces` anyway.
.. sidebar:: API Documentation
* :php:trait:`OCC\Basics\Traits\Getter`
2024-03-26 20:10:30 +01:00
Getter
======
2024-03-31 17:41:04 +02:00
*Reads data from inaccessible properties by using magic methods.*
To make a `protected` or `private` property readable, provide a method named `_magicGet{Property}()` which handles the
reading. Replace `{Property}` in the method's name with the name of the actual property (with an uppercase first
letter).
Trying to access an undefined property or a property without corresponding "magic" getter method will result in an
`\InvalidArgumentException <https://www.php.net/invalidargumentexception>`_.
Example: If the property is named `$fooBar`, the "magic" method has to be `_magicGetFooBar()`. This method is then
called when `$fooBar` is read in a context where it normally would not be accessible.
.. sidebar:: API Documentation
* :php:trait:`OCC\Basics\Traits\Setter`
Setter
======
*Writes data to inaccessible properties by using magic methods.*
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
To make a `protected` or `private` property writable, provide a method named `_magicSet{Property}()` which handles the
writing. Replace `{Property}` in the method's name with the name of the actual property (with an uppercase first
letter).
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
Trying to access an undefined property or a property without corresponding "magic" setter method will result in an
`\InvalidArgumentException <https://www.php.net/invalidargumentexception>`_.
Example: If the property is named `$fooBar`, the "magic" method has to be `_magicSetFooBar()`. This method is then
called when `$fooBar` is written to in a context where it normally would not be accessible.
.. sidebar:: API Documentation
* :php:trait:`OCC\Basics\Traits\OverloadingGetter`
2024-03-26 20:10:30 +01:00
OverloadingGetter
=================
2024-03-31 17:41:04 +02:00
*Overloads a class with readable virtual properties.*
It reads a protected internal array whose keys are interpreted as property names.
Trying to access an undefined virtual property will not issue any warning or error, but return `NULL` instead.
Example: Reading `Foo->bar` will return the value of `Foo::$_data['bar']`.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
.. note::
Internally it uses the `$_data` array, the same as some :doc:`datastructures` and all :doc:`interfaces` of this
package.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
.. sidebar:: API Documentation
* :php:trait:`OCC\Basics\Traits\OverloadingSetter`
2024-03-26 20:10:30 +01:00
OverloadingSetter
=================
2024-03-31 17:41:04 +02:00
*Overloads a class with writable virtual properties.*
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
It writes a protected internal array whose keys are interpreted as property names.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
Trying to access a previously undefined virtual property will create a new one with the given name.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
Example: `Foo->bar = 42;` will set `Foo::$_data['bar']` to `42`.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
.. note::
Internally it uses the `$_data` array, the same as some :doc:`datastructures` and all :doc:`interfaces` of this
package.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
.. sidebar:: API Documentation
* :php:trait:`OCC\Basics\Traits\Singleton`
2024-03-26 20:10:30 +01:00
Singleton
=========
2024-03-31 17:41:04 +02:00
*Allows just a single instance of the class using this trait.*
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
Get the singleton by calling the static method `getInstance()`. If there is no object yet, the constructor is called
with the same arguments as `getInstance()`. Any later call will just return the already instantiated object
(irrespective of the given arguments).
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
.. caution::
In order for this to work as expected, the constructor has to be implemented as `private` to prevent direct
instantiation of the class.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
.. sidebar:: API Documentation
* :php:trait:`OCC\Basics\Traits\TypeChecker`
2024-03-26 20:10:30 +01:00
TypeChecker
===========
2024-03-31 17:41:04 +02:00
*A generic data type checker.*
This allows to set a list of allowed atomic data types and fully qualified class names. It also provides a method to
check if a value's data type matches at least one of these types.
2024-03-26 20:10:30 +01:00
2024-03-31 17:41:04 +02:00
Available atomic types are `array`, `bool`, `callable`, `countable`, `float` / `double`, `int` / `integer` / `long`,
`iterable`, `null`, `numeric`, `object`, `resource`, `scalar` and `string`.