Download the PHP package xp-forge/partial without Composer
On this page you can find all versions of the php package xp-forge/partial. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xp-forge/partial
More information about xp-forge/partial
Files in xp-forge/partial
Package partial
Short Description Partial types
License BSD-3-Clause
Homepage http://xp-framework.net/
Informations about the package partial
Partials: Compile-time metaprogramming
For situations where more logic than just "compiler-assisted copy&paste" using PHP's traits is necessary, this library provides a syntax that expand dynamically based on the containing class at compile time.
Partial flavors
The partials provided by this library's are divided in two flavors: Kinds and composeables.
- Kinds define the general concept of a type. You can say, e.g.: This type is a list of something, or a reference to something. Or, to use more concrete examples: The
Customers
class is a list of customers (encapsulated byCustomer
instances), andName
is a reference to (a string) containing a name. - Composeables can be used alone or in combination to extend a base type or a kind. You can say, e.g. This type comes with a certain functionality. Again, using a realistic use-case: The
Person
class comes withtoString()
,compareTo()
andhashCode()
methods.
Regardless of their flavor, some partials are actually implemented by a regular PHP trait, others are dynamically created at runtime. However, the syntax for both is use [Containing-Type]\[is-or-with]\[Partial-Name]
.
Walk-through
The Box
trait creates a value object wrapping around exactly one member. It creates a one-arg constructor, and a value()
for retrieving the value, and includes appropriate hashCode()
, compareTo()
and toString()
implementations.
Writing this:
...is equivalent to:
The parametrized Accessors
trait creates accessors for all instance members.
Writing this:
...is equivalent to:
If the constructor consists solely of assignments, you can include the Constructor
trait and remove it. The parameters will be declared in the order the fields are declared: top to bottom, left to right in the source code.
Writing this:
...is equivalent to:
To combine all these, you can use the Value
trait, which a) creates a constructor with all members as parameters, b) accessors for reading these, and c) implements the hashCode()
, compareTo()
and toString()
methods.
The ListOf
trait creates a list of elements which can be accessed by their offset, iterated by foreach
, and offers equals()
and toString()
default implementations.
Writing this:
...is equivalent to:
The Builder
trait will add a static with()
method to your class, generating a fluent interface to create instances. This is especially useful in situation where there are a lot of constructor parameters.
The Comparators
trait adds static by[Member]
methods returning util.Comparator instances for each member. These instances can be combined using then (Post::byDate()->then(Post::byAuthor())
) or reversed (Post::byDate()->reverse()
).
The ListIndexedBy
trait creates a list of elements which can be queried by name, also overloading iteration and creating equals()
and toString()
in a sensible manner. The class needs to implement the abstract index
method and return a string representing the name.
Putting it all together, we can see the API:
See also
All versions of partial with dependencies
xp-forge/mirrors Version ^6.0 | ^5.0 | ^4.0 | ^3.0
php Version >=7.0.0