What does a colon mean in PHP?
I recently started learning PHP.
There was a sample of the Slim framework in the book, so I am copying the sutra while understanding the meaning of it.
Therefore, I have some questions.
↓Slim Sample Application With the Tinter code ,
$app->get('/', '\Tinitter\Controller\TimeLine:show');
and
TimeLine: Why is there only one colon in show
??
TimeLine
I'm calling the class method show
, but isn't it TimeLine::show
??
In the case of instance method, I think it is called by the Arrow operator, but in the case of class method, do you mean that :
is called between :
instead of :
?
This is Slim's own expression and has nothing to do with PHP.
This is the routing (sorting) part of the process by URL.Determines the URL pattern and specifies what to do with the matching pattern.
$app->get('/', '\Tinitter\Controller\TimeLine:show');
→ If /
has HTTP access, it is assumed that TimeLine
is instantiated and the show
method is used as the method to handle HTTP.It's PHP-like to instantiate it carefully for each access.
The method of specifying the instance method in :
seems to have been added to Slim 2.4.0 and does not appear in document, so it is only natural that you should be questioned.The release notes also mention that the , :
, appears to be a static method call.
Note: Slim Framework Document:RouteParameters
$app->get('/', '\Tinitter\Controller\TimeLine:show');
The '\Tinitter\Controller\TimeLine:show'
part of this code is simply a "string" for the PHP language structure.The class and method correspond to it, so it looks like PHP's language structure, which is confusing, but PHP doesn't interpret anything from this string.
When you invoke $app->get()
in the Slim framework code as described above, the setCallable()
method of the Route
class in the framework is called.
public function setCallable($callable)
{
$matches=array();
if(is_string($callable)&preg_match('!^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!',$callable,$matches)){
$class=$matches[1];
$method = $matches[2];
$callable=function()use($class,$method){
static$obj=null;
if($obj===null){
$obj = new $class;
}
return call_user_func_array(array($obj,$method), func_get_args());
};
}
if(!is_callable($callable)){
through new\InvalidArgumentException('Route callable must be callable');
}
$this->callable=$callable;
}
If you look at this code, you can see that if the specified one is a string and there is a colon, you divide it into class names and methods, and then instantiate the class.
© 2024 OneMinuteCode. All rights reserved.