Download the PHP package phpfn/symbol without Composer
On this page you can find all versions of the php package phpfn/symbol. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package symbol
Short Description The Symbol type implementation
License MIT
Homepage https://github.com/phpfn
Informations about the package symbol
Symbol
Symbol is a special primitive data type, indicating an unique identifier.
This symbol
library implementation is similar to alternative types in:
1) Symbols in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol 2) Symbols in Ruby: https://ruby-doc.org/core-2.5.0/Symbol.html 3) Symbols in TypeScript: https://www.typescriptlang.org/docs/handbook/symbols.html 4) Atoms in Erlang: http://erlang.org/doc/reference_manual/data_types.html#atom 5) ...e.g.
Installation
Library can be installed into any PHP application:
In order to access library make sure to include vendor/autoload.php
in your file.
Usage
In order to create a new symbol data type, you should use
the Symbol::create()
method or call the global symbol()
helper function.
Names
Each symbol can have a name (description), which is passed as the first parameter.
Uniqueness
Please note that the symbols are unique regardless of the names.
Usage In Constants
Symbols can be used as variable values and even constant!
Serialization
However cannot be serialized:
Type Comparison
Notice that the symbols are neither a string, nor a number, nor anything (almost :3) else.
Clone
Note that symbols are always passed by reference and cloning not allowed.
Naming
And in order to get the name of a symbol,
just use the Symbol::key()
method:
Reflection
And you can find out some details about this type:
Global Symbols
In addition to all this, you can use the Symbol::for
method to create
a global symbol. The Symbol::for($key)
method searches for existing symbols
in a runtime-wide symbol registry with the given key and returns it if
found. Otherwise a new symbol gets created in the global symbol registry
with this key.
And method Symbol::keyFor($symbol)
returns a name
for a global symbol.