How to Use PHP Reserved Names for My Functions and Classes

Can I use PHP reserved names for my functions and classes?

No, you can't. Thank god.

PHP reserved method names

You can't use language construct names in a classes regarding namespaces, while you can use built-in function names in namespace. A namespace in this case is a method of a class.

$foo = new Foo();

$foo->array_key_exists(); // Good
$foo->isset(); // Fatal error, since isset() a language construct
$foo->include(); // Also fatal error

The reason you don't get a fatal error when using min() is because its a built-in function.

Generally it's considered bad practice using built-in names in your own implementations because that might confuse another developers that will be reading your code in future.

How to use builtin function name inside a class as a method name

Nope, PHP doesn't have a way of escaping reserved words so that they can be used as class/method/etc names.

You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion.

As of PHP 7.0.0 these keywords are allowed as as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.

See http://php.net/manual/en/reserved.keywords.php for a full list.

Demo: https://3v4l.org/WVgvh

PHP reserved words as namespaces and class names

PHP will throw an error like:

Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING

if you try:

class Array
{

}
$myClass = new Array;

Instead, try something like

class myArray
{

}
$myClass = new myArray;

[edit] I'll elaborate on that a little, they're reserved for a reason because in the first scenario above, PHP wouldn't be able to tell the difference between you defining an array or initialising a class of the same name, so it throws an error. There's no way round this like in MySQL, for example, where you can escape reserved words with a backtick. So in PHP you're forced to change the name, you don't have to change it much, one char will do as long as you're not using the exact same name as a reserved word.

Hope that helps!

Why I can't create a class named Case on PHP

Because case is the reserved word.

Reserved words can not be used as constants, function names, class names and etc. Try to avoid that.

How can I check whether a word is reserved by PHP?

Array borrowed from http://www.php.net/manual/en/reserved.keywords.php

You could easily modify it to work for the predefined constants array.

This works.

<?php
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');

$checkWord='break'; // <- the word to check for.
if (in_array($checkWord, $keywords)) {
echo "Found.";
}

else {
echo "Not found.";
}

?>

You could also implement this in conjunction with a form by replacing:

$checkWord='break';

with

$checkWord=$_POST['checkWord'];

I.e.:

<?php

if(isset($_POST['submit'])){
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
$checkWord=$_POST['checkWord'];

if (in_array($checkWord, $keywords)) {
echo "FOUND!!";
}

else {
echo "Not found.";
}

}

?>

<form method="post" action="">

Enter word to check:
<input type="text" name="checkWord">

<br>
<input type="submit" name="submit" value="Check for reserved word">
</form>

A different version using both arrays set inside a form.

It could stand for some polishing up, but it does the trick

<?php

if(isset($_POST['submit'])){
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');

$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');

$checkWord=$_POST['checkWord'];

$checkconstant=$_POST['checkconstant'];

if (in_array($checkWord, $keywords)) {
echo "<b>Reserved word FOUND!!</b>";
echo "\n";
}

else {
echo "Reserved word not found or none entered.";
}

if (in_array($checkconstant, $predefined_constants)) {
echo "<b>Constant word FOUND!!</b>";
echo "\n";
}

else {
echo "Constant not found or none entered.";
}

}

?>

<form method="post" action="">

Enter reserved word to check:
<input type="text" name="checkWord">

Enter constant word to check:
<input type="text" name="checkconstant">

<br><br>
<input type="submit" name="submit" value="Check for reserved words">
<input type="reset" value="Reset" name="reset">
</form>

Laravel 5.7: Defining Relationships with Models having Reserved Names

you can't use reserved words!!!!

As a suggestion (what I did in my project) is use the name for the model 'ClassRoom' or something.

And the name for the relationship 'classRooms()' or something.

Is it possible to name a constant a protected word in PHP?

Nope, not possible, at least not for class constants.

You cannot use any of the following [reserved] words as constants, class names, function or method names.

I don't know about C#, but there isn't any special symbol in PHP to transform a keyword into an identifier. As long as you don't name it exactly the same as a keyword (barring letter case), it'll just be any normal constant name.

How about a (different since it's not just CSS) prefix? Gets repetitive to type, but is a nice workaround. I realize this may be redundant as well if your class is named something like HTMLAttribute, but it's the easiest way out.

const A_ID = 'id';
const A_CLASS = 'class';
// etc

Can't have function named default in php

You cannot use a reserved word as a class function. You can, however, use __call:

<?php
abstract class Controller {

public function _default($some, $args) {}

public function __call($name, $arguments) {
if ($name === 'default') {
//return call_user_func_array(array($this, '_default'), $arguments); // has args
return $this->_default(); // simpler
}
}
}

Which will permit you to do $foo->default(). A better idea though would be to not choose an implementation/api that relies upon using reserved words for method names.



Related Topics



Leave a reply



Submit