Code Monkey home page Code Monkey logo

symbol's Introduction

Symbol

Travis CI

PHP 7.1+ Latest Stable Version Total Downloads License MIT

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.

Usage

In order to create a new symbol data type, you should use the Symbol::create() method or call the global symbol() helper function.

<?php

$a = symbol();

// OR

$b = \Serafim\Symbol\Symbol::create();

Names

Each symbol can have a name (description), which is passed as the first parameter.

<?php

$symbol = symbol('id');

Uniqueness

Please note that the symbols are unique regardless of the names.

<?php

var_dump(symbol() === symbol()); 
// expected output: false

var_dump(symbol('example') === symbol('example')); 
// expected output: false

Usage In Constants

Symbols can be used as variable values and even constant!

<?php

define('EXAMPLE', symbol());

var_dump(is_symbol(EXAMPLE));
// expected output: true

class Example
{
    public const CLASS_CONST = EXAMPLE;
}

var_dump(is_symbol(Example::CLASS_CONST));
// expected output: true

Serialization

However cannot be serialized:

<?php

serialize(symbol());
// Error

Type Comparison

Notice that the symbols are neither a string, nor a number, nor anything (almost :3) else.

<?php

var_dump(\is_string(symbol()));
// expected output: false

var_dump(\is_int(symbol()));
// expected output: false

var_dump(\is_float(symbol()));
// expected output: false

var_dump(\is_bool(symbol()));
// expected output: false

var_dump(\is_array(symbol()));
// expected output: false

var_dump(\is_object(symbol()));
// expected output: false

var_dump(\is_null(symbol()));
// expected output: false

var_dump(\is_symbol(symbol()));
// expected output: true

var_dump(\is_resource(symbol()));
// expected output: true

Clone

Note that symbols are always passed by reference and cloning not allowed.

<?php

$a = symbol();
$b = $a;

var_dump($a === $b);
// expected output: true

var_dump(clone $a);
// Error

Naming

And in order to get the name of a symbol, just use the Symbol::key() method:

<?php
use Serafim\Symbol\Symbol;

var_dump(Symbol::key(symbol('hello')));
// expected output: "hello"

var_dump(Symbol::key(symbol('hello')) === Symbol::key(symbol('hello')));
// expected output: true

var_dump(symbol('hello') === symbol('hello'));
// expected output: false

Reflection

And you can find out some details about this type:

<?php

use Serafim\Symbol\Symbol;

$reflection = Symbol::getReflection(Symbol::create('hello'));

$reflection->getName();      // Contains "hello" string
$reflection->getFileName();  // Provides path/to/file-with-symbol-definition.php
$reflection->getStartLine(); // Provides definition line

// etc...
 

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.

<?php

use Serafim\Symbol\Symbol;

var_dump(Symbol::for('a') === Symbol::for('a'));
// expected output: true

var_dump(Symbol::create('a') === Symbol::for('a'));
// expected output: false

And method Symbol::keyFor($symbol) returns a name for a global symbol.

<?php

var_dump(Symbol::keyFor(Symbol::for('a')));
// expected output: "a"

var_dump(Symbol::keyFor(Symbol::create('a')));
// expected output: null

symbol's People

Contributors

serafimarts avatar

Stargazers

R1KO avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.