PHP code example of phpshots / common-type-alias

1. Go to this page and download the library: Download phpshots/common-type-alias library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpshots / common-type-alias example snippets


     

    use PHPShots\Common\TypeAlias;

    // Create an instance of TypeAlias
    $typeAlias = new TypeAlias;

    // Create a type alias
    $typeAlias->alias('MyAlias', 'OriginalType');

    // Example of an original type
    class OriginalType {
        public function sayHello(string $type) {
            return "Hello from $type!";
        }
    }

    // Instantiate using the alias
    $myVariable = new MyAlias();
    echo $myVariable->sayHello($typeAlias->getAlias('MyAlias')); // Outputs: Hello from OriginalType!
    

      // Check if a name is an alias
      if ($typeAlias->isAlias('MyAlias')) {
          echo "MyAlias is an OriginalType.\n";
      }
    

    // Remove an alias
    $typeAlias->removeAbstractAlias('MyAlias');
    

    // Retrieve the ultimate alias
    echo "Ultimate alias for MyAlias: " . $typeAlias->getAlias('MyAlias') . "\n";
    

    // Register some aliases
    $typeAlias->alias('SomeClass', 'Alias1');
    $typeAlias->alias('Alias1', 'Alias2'); // chaining alias
    

    // Register some aliases in reversed order
    $typeAlias->alias('Alias1', 'SomeClass');
    $typeAlias->alias('Alias2', 'Alias1'); // chaining alias
    

    // List all aliases for a type
    print_r($typeAlias->getAllAliases('SomeClass'));
    

    // Clear all aliases
    $typeAlias->clearAliases();
    

    // Display all direct mappings
    print_r($typeAlias->getAliasMap());  
    

    // Check for aliases of an abstract type
    echo $typeAlias->hasAbstract('SomeClass') ? "SomeClass has aliases.\n" : "No aliases for SomeClass.\n";

    
sh
  apt install php