A friend writes the engine in php. We had a big dispute with him about the use of a hyphen (in url, folder names for models / controllers / viewers, etc.). So, he says that php "crashes" at the sight of this sign. As a result, the url of the form some-interesting-url.com does not work. He wanted to use a dash as a result. There was a dispute that such game is not used in url. He made some kind of "crutch", which replaces the hyphen on the dash. Let's say it works. But in the end, folders and links to its functions still remain with a dash. An example of a reference to a function on the screen. Judge two people, and help find a competent solution to this problem. Thanks in advance) enter image description here

  • What's the difference between a hyphen and a dash? - rjhdby
  • this is a hyphen (-), and this is a dash (-) - zh-mskl9
  • @rjhdby is different typographic symbols artlebedev.ru/kovodstvo/sections/97 - etki
  • one
    It seems to your friend that it is too early to call yourself a programmer. - etki
  • one
    @ zh-mskl9, just some kind of game. Why "php crashes", what a dash ... But let him rave than he wants, maybe someday and learn something. - PinkTux

2 answers 2

No, you can not use "-" (hyphen) in the name of a function (method) or class property!

The function / method / property of an object / variable may begin with the letter / underscore [A-Za-z_\x7f-\xff] and may contain the digits [0-9] .

UPD: as correctly noted in the comments: the name may contain bytes from 127 to 255, but it is probably NOT RECOMMENDED to use this !

You can try to bypass the restriction (for properties, not for methods):

 class test { function __construct() { $this->{'have-dash'} = ''; } } $test = new test(); $test->{'have-dash'} = ':D'; 

IMHO, this is all, at least stupid.

We had a big dispute with him about the use of a hyphen (in url, folder names for models / controllers / viewers, etc.).

You can use ^ 1 in url, in php - it is not possible, as in folder names, etc. - undesirable.

^ 1: provided that it is necessary (for CNC, for example) and then converted to the desired form (that is, it is not used in the form that was used for php names).

UPD: by CNC means: enter image description here
There is still a lot of controversy about SEO and url with a hyphen or underscore ....

Judge two people, and help find a competent solution to this problem. Thanks in advance)

To use "_" , it considers it a good tone rule (in js, for example, it is customary to use camelCase).

Ps. In general, it is better to use smart routing and then such problems will disappear. (it is not necessary straight to url in the name of the function of the route)

  • You are wrong about the valid characters in the titles. php allows any characters from the top of the table. php.net/manual/ru/language.variables.basics.php including any two or more UTF-8 byte characters, including a dash (consisting of 0xE2 0x80 bytes). And the dash or minus sign - (code 0x2D) is from the bottom of the table and, of course, is not included in the range of allowed characters. - Mike
  • one
    provided that it is necessary (for CNC, for example) and then converted to the desired form (ie, is not used in the form that was used for php names). - what is the general relation of the URL to the naming within the language? Using the "_", it considers the rule of good tone - you are familiar with the PSR? - etki 4:02 pm
  • @Etki author routing straight from url to f-tions / methods name))) is familiar. updated answer. - sanix
  • The method name can be with dashes, only such a method should be processed in __call - vp_arth

Note, use the rules of the standard PSR-x, native to PHP.

Russian translation from the link below:

https://svyatoslav.biz/misc/psr_translation/

  • Link must be direct. Correct, until spam is not noted. - Qwertiy
  • Direct - this is the one that leads directly to the desired site. - Qwertiy