In a CakePHP Plugin documentation there is the following code line: $validator->provider('upload', \Josegonzalez\Upload\Validation\DefaultValidation::class);
\Josegonzalez\Upload\Validation\DefaultValidation is the namespace, but I didn't understand the ::class. Could someone explain it? I didn't find anything in PHP documentation.
the class constant simply returns the full name of the class (with namespace) as a string. So instead of passing as string to some method that requires it, you pass it the PHP way. It just looks nice, for example:
$validator->provider('upload',\Josegonzalez\Upload\Validation\DefaultValidation::class);
AND
$validator->provider('upload', '\Josegonzalez\Upload\Validation\DefaultValidation');
Both Are Same
And another advantage of this is that, if you need full class name several times in a single file.. say onto multiple method calls as a parameter. You can simply use it on the top & then only the classname will return the full name with namespace. like this:
use \Josegonzalez\Upload\Validation\DefaultValidation;
$validator->provider('upload', DefaultValidation::class);
//you can use it on other places as well, if required.
$someOtherClass->someOtherMethod(DefaultValidation::class);
So, in short, it reduces the number of characters you need to type, and makes your code look cleaner.
Related
I am using Advanced Parser I made this code that extracts the value of the class "text-brand"
However, I want to make the search more exact. Like this but it doesn't work.
"//*[#class='text-brand']", //This way it works but it's not so exact
How can I make it detect the subclass?
"//*[#class='product-price text-brand']",
In this 2nd code I say that the main class is this
"product-price" and that "text-brand" is inside it.
What am I doing wrong?
As an additional note I am using the famous variable $this->xpathScalar($paths)
Solved - Valid answer:
//div[#class='product-price']/ins/span[#class='text-brand']
Essentially, I seek to pass a static class method to a callback, but do not wish to do so using a hard-coded string, but rather the fully-qualified class method literal. We can do that using classes like so:
$name = NS\FooClass::class;
instead of:
$name = 'NS\FooClass';
which will give us the string of the fully-qualified name of the class. I seek to be able to do something similar for a class method like so:
$name = NS\FooClass::foo_method::method;
instead of:
$name = 'NS\FooClass::foo_method';
It is more manageable and I can use the IDE functionality way better using the literals. Any similar way I can achieve what I want with the class methods without using strings?
There is currently no such mechanism built into the language. It has been suggested - see for instance this discussion from Feb 2020 - but there are more nuances to think about than might be immediately apparent; notably:
Should the syntax resolve at run-time and check the existence of the class and the method (::class in most cases doesn't; a bare function like strlen::func would have to because of the way namespaces resolve; an object implementing __callStatic could never be used this way)?
Should the result be a string, an array (see below), or a Closure object?
Anyway, that's a topic for elsewhere...
As the manual page on the callable type says, there are two ways to specify a static method for use as a callback:
As a string, as in your example 'NS\FooClass::foo_method'
As an array where the first part is a class name, and the second part is a the method name: ['NS\FooClass', 'foo_method']
Since only the class name needs to be qualified with namespace information, you can use ::class with the second syntax to get nearly what you wanted:
$callback = [NS\FooClass::class, 'foo_method'];
This allows any decent IDE to spot the reference to the class, and allows you to reference it by an imported or aliased name.
It's worth noting that if the callable type is specified in a parameter or return type declaration or a docblock, some IDEs (e.g. PhpStorm) will "understand" either format as a reference to the method, and include it in features like "find usages" and "go to declaration".
So we can use expressions to transform other variables in live templates.
For example:
Is it possible to apply snakeCase to NAME directly? So whatever I type, gets converted into snake case? Desired result:
Tried snakeCase(NAME), snakeCase(String) and snakeCase(). None seemed to work. Maybe someone had it figured out?
No, it's not possible - you can't pass a variable to itself, it has to be either another live template variable (defined before) or some known value calculated based on clipboard content (snakeCase(clipboard())), file name (snakeCase(fileName())), name got from completion, etc.
If you like to change names of existing variables, you can try String Manipulation plugin, for example
In PHP, call_user_func(array(self, 'method_name')) doesn't work. The self keyword cannot be used in that context. I need to actually include the name of the class call_user_func(array('class_name', 'method_name')).
However, if I'm not in a static function, the $this variable does work in that context. Why the difference?
If you want the name of the current class context, use get_class() (without any parameters) or __CLASS__.
You've already written the difference; self is a keyword, and is not usable as a reference in an array (what kind of type should that be in PHP?). get_class() returns a string, and the array()-callback supports using a string as the first name to do a static call.
You can try with __CLASS__ to get the class name. Also it may work to use call_user_func('self::method_name') directly, but I didn't test it and the documentation about callbacks doesn't say anything about this.
self is just an undefined constant, so it expresses 'self'. So these two are the same:
array(self, 'method_name');
array('self', 'method_name');
And depending on the PHP version you use, this actually works, see Demo.
In case it does not work with your PHP version, some alternatives are:
call_user_func(array(__CLASS__, 'method_name'));
or
call_user_func(__CLASS__.'::method_name'));
or (in case you don't need call_user_func):
self::method_name();
Since PHP 5.5, you can do [self::class, 'methodName'].
::class is really useful for situations where you have a class name (maybe a local alias) and you need to generate the full class name as a string.
In PHP 5.3, you can write call_user_func('self::method') or call_user_func(array('self', 'method')). I suppose the latter could work in older versions as well.
Are there any circumstances where identically named classes and functions in PHP, could collide or cause problems in any way? For example:
function Foobar(){
// ...
}
class Foobar{
// ...
}
Cursory testing shows that PHP can discern between them based on context.
No, they never collide. But:
Do not do it.
You will confuse everyone if you do so, because I would not expect there to be a function and a class of the same name. Many don't even know it's legal to do so.
When I see an upper case name In PHP (first letter), I assume it is a class. If you put () around it, I will know it's a function. But I wouldn't assume that there is a class of the same name. All you do is confuse people. Some might assume: "Cool, I didn't know you could omit new". I don't know what your intents are, but if it's to get rid of the new keyword - and only that - it's very bad. I will assume you do more than just that, and will go check what that function actually does, and I'll get angry if I find out it does nothing except returning a new instance without doing anything... I just wasted my time looking up a function that does... nothing.