Adam Biro

Software Developer

Blogger

Photographer

Full Stack Developer

Photo Enthusiast

Web Developer

Adam Biro
Adam Biro

Software Developer

Blogger

Photographer

Full Stack Developer

Photo Enthusiast

Web Developer

Blog Post

[8] PHP8 new get_resource_id, token_get_all and Abstract methods

May 6, 2022 IT, PHP, Programming
[8] PHP8 new get_resource_id, token_get_all and Abstract methods

This is the eighth part of the PHP8 series and in the current post, I want to go through the get_resource_id and token_get_all functions and the Abstract methods in traits improvements. You can see the full list of featuresĀ here. Furthermore, you can read the seventh part of the seriesĀ here.

The get_resource_id function

The resource is a special PHP variable. It is referring to external resources. One example is a MySQL connection, another one is the file handle.

When we are working with resources then PHP generates an ID for every resource. The type of this identifier before PHP8 was a string and we had to typecast it to an integer. So in order to avoid the typecast the new get_resource_id function can help. This is the new type-safe way to get the identifier of the resource.

// Before PHP 8.0 we had to typecast the resource identifier
$resourceId = (int) $resource;
// In PHP 8.0 we can use the get_resource_id function without typecasting.
$resourceId = get_resource_id($resource);

The token_get_all function

The token_get_all function is available since PHP4.2. At the moment the function definition according to the documentation is the following:

  • Split a given source into PHP tokens
  • It parses the given source string into PHP language tokens using the Zend engine’s lexical scanner.
<?php
$tokens = token_get_all('<?php echo; ?>');
foreach ($tokens as $token) {
    if (is_array($token)) {
        echo "Line {$token[2]}: ", token_name($token[0]), " ('{$token[1]}')", PHP_EOL;
    }
}
?>
/* 
  The output of the code above:
  Line 1: T_OPEN_TAG ('<?php ')
  Line 1: T_ECHO ('echo')
  Line 1: T_WHITESPACE (' ')
  Line 1: T_CLOSE_TAG ('?>')
*/

As we can see from the code above. PHP understands the input string syntax and it parses the string based on that. We can find the List of Parser Tokens here that PHP be able to identify in the input string.

Okay, but what is the new feature if the function is available from PHP 4.2?

Developers added a new PhpToken class into the language with a PhpToken::getAll() method. This implementation works with objects instead of plain values and the benefit is that it consumes less memory and is easier to read.

The Abstract methods in traits improvements

Traits can specify abstract methods but they must be implemented by the class which is using them. Before PHP 8.0 this method was not validated. So we could write a code like this without getting any errors:

trait ExampleTrait {
    abstract public function Example(int $input): int;
}
class UsesExampleTrat
{
    use ExampleTrait;
    public function Example($input)
    {
        return $input;
    }
}

But in PHP 8.0 we can not do that. We need to write this instead of the code above:

class UsesTrait
{
    use ExampleTrait;
    public function Example(int $input): int
    {
        return $input;
    }
}

Taggs:
Write a comment