Download the PHP package stubbles/reflect without Composer

On this page you can find all versions of the php package stubbles/reflect. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package reflect

stubbles/reflect

Reflection helper functions and annotations.

Build status

Tests

Latest Stable Version

Installation

stubbles/reflect is distributed as Composer package. To install it as a dependency of your package use the following command:

composer require "stubbles/reflect": "^10.0"

Requirements

stubbles/reflect requires at least PHP 8.2.

Additionally it uses stubbles/sequence to return sequences from some of the functions, and stubbles/values to parse values from annotations.

Reflection helper functions

All functions are in namespace stubbles\reflect.

reflect()

Available since release 3.1.0

To provide more convenience a function stubbles\reflect\reflect() is provided. It allows to reflect classes, objects and methods:

Since release 4.0.0, it also allows to reflect functions: `

reflectConstructor(): \ReflectionMethod

Shortcut for reflect($someObject, '__construct')

methodsOf($class, $filter = null): \stubbles\sequence\Sequence

Returns a sequence of all methods of given class.

propertiesOf($class, $filter = null): \stubbles\sequence\Sequence

Returns a sequence of all properties of given class.

parametersOf($classOrFunction, $methodName = null): \stubbles\sequence\Sequence

Returns a sequence of all properties of given class.

parametersOfConstructor($class): \stubbles\sequence\Sequence

Shortcut for parametersOf($classOrFunction, '__construct').

parameter($name, $classOrFunction, $methodName = null): \ReflectionParameter

Returns parameter with given name from referenced function or method.

constructorParameter($name, $class): \ReflectionParameter

Shortcut for parameter($name, $class, '__construct').

Annotations

For details about the concept behind annotations see annotations which contains a general introduction into the topic. For more details how to use this in a programming language see Java annotations.

How to define an annotation

Annotations can be defined on every element that can get a docblock comment: functions, methods, classes and properties. Additionally it is possible to define annotations for parameters in the docblock comment of the function or method where the parameter is part of.

In the above example you can see five different ways of defining an annotation. However, you can combine these ways as you like. You may have a casted annotation with no, one or more values. But lets go through all defined annotations.

In contrast to other implementations it is not required to create a separate class for the annotation.

Read annotations

To get a list of all annotations of an annotatable element call the stubbles\reflect\annotationsOf() function. It returns an instance of stubbles\reflect\annotation\Annotations and supports the following invocations:

As convienience function to retrieve annotations of a class' constructor the function stubbles\reflect\annotationsOfConstructorParameter() can be used:

As convienience function to retrieve annotations of a function or method parameter the function 'stubbles\reflect\annotationsOfParameter()` can be used:

Read values from annotations

As seen above annotations can have values. If we take the @AnnotationWithValues from the class above this is how you can access them:

These are the possibilities:

In case the annotation has an unnamed value only it can be retrieved using $annotation->value or $annotation->getValue().

Using the method syntax with get has the advantage that a default value can be supplied which will be returned in case a value with this name is not set:

The default value for the is method syntax is false.

If values are optional their existence can be checked: $annotation->hasValueByName('bar') returns true if a value with the name bar is set, and false otherwise.

When parameter values are read they are parsed using stubbles\values\Parse from stubbles/values. See documentation of this package how values are parsed.

Annotation cache

As parsing annotations is quite expensive Stubbles will cache annotations once they are read. However, the cache by default works only on a per request base and is not persistent. In order to make the annotation cache persistent between different requests the application needs to provide an annotation cache persistence.

The simplest way to do so is to call stubbles\reflect\annotation\persistAnnotationsInFile('/path/to/some/cachefile.cache') which will then use the file named in the argument to persist the annotation cache inbetween requests.

If a cache file doesn't suit your needs you can also provide a different persistence. To do so, you need to call stubbles\reflect\annotation\persistAnnotations() and provide two functions, one which is able to read the data and return it, and another one which can receive data and store it. For a file cache implementation, this would look like this:

The first function must return the stored annotation data. If no such data is present it must return an empty array.

The second function must store passed annotation data.

Please note that if you use a persistent annotation cache that you need to be able to clear this cache, as changes in your code on annotations will not lead to an update of the cached data. This might lead to confusion on why a changed annotation will not be respected when the code is executed.


All versions of reflect with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
stubbles/sequence Version ^10.1
stubbles/values Version ^11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package stubbles/reflect contains the following files

Loading the files please wait ....