0

I'm looking for a way to replace __NAMESPACE__ in the following code:

namespace classes\utility;
class Cleanse
{
    public static function escape($values)
    {
        return is_array($values) ?
                    array_map(__NAMESPACE__.'\Cleanse::escape', $values) :
                    htmlentities($values, ENT_QUOTES, 'UTF-8');
    }
}

From doing some reading, I tried this:

public static function escape($values)
{
    return is_array($values) ?
                array_map([static::class, 'escape'], $values) :
                htmlentities($values, ENT_QUOTES, 'UTF-8');
}

but got this error:

Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
and it referred to the array_map line of code.

I'm looking for a solution that's compatible with PHP version 5.4.4 and above, and something that is not going to be deprecated any time soon.

EDIT: I was unable to find the answer in the proposed duplicate post because it is so long. Therefore, I do not agree that this is a duplicate posting.

knot22
  • 2,648
  • 5
  • 31
  • 51

3 Answers3

3

Because you've said you're using PHP 5.4, you will need to use http://php.net/manual/en/function.get-called-class.php

Replace

static::class

With

get_called_class()

Because the static::class syntax was added in version 5.5.0

http://php.net/manual/en/language.oop5.changelog.php

5.5.0 Added: The ::class magic constant.

Scuzzy
  • 12,186
  • 1
  • 46
  • 46
1

Simply use the __METHOD__ magic constant as this is a recurisve call.

namespace classes\utility;

class Cleanse
{
    public static function escape($values)
    {

        //array_map(__CLASS__.'::escape', $values) :
        //array_map(__CLASS__.'::'.__FUNCTION__, $values) :
        //etc... all work
        return is_array($values) ?
                    array_map(__METHOD__, $values) :
                    htmlentities($values, ENT_QUOTES, 'UTF-8');
    }
}

$C = new Cleanse();
print_r( $C->escape(["foo"]) );

Output

['foo']

Sandbox -note- you can change PHP versions in the sandbox, for example changing it to 5.4

If you are worried about overwriting and late static binding then you have to use get_called_class no matter what. But that shouldn't be an issue with a recursive call one would think, as if you overwrite it, then it is still the method and it doesn't access any internal data to the class.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0
<?php
class MyClass {
    const CONST_VALUE = 'A constant value';
}

$classname = 'MyClass';
echo $classname::CONST_VALUE; // As of PHP 5.3.0

echo MyClass::CONST_VALUE;
?>

Three special keywords self, parent and static are used to access properties or methods from inside the class definition.

Example #2 :: from inside the class definition

<?php
class OtherClass extends MyClass
{
    public static $my_static = 'static var';

    public static function doubleColon() {
        echo parent::CONST_VALUE . "\n";
        echo self::$my_static . "\n";
    }
}

$classname = 'OtherClass';
$classname::doubleColon(); // As of PHP 5.3.0

OtherClass::doubleColon();
?>

http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php

zod
  • 12,092
  • 24
  • 70
  • 106