D
D
DarkByte20152017-11-17 12:08:35
React
DarkByte2015, 2017-11-17 12:08:35

Static use methods?

Is there anything like this in puff? To then use static methods without specifying the class name. Many languages ​​have this ... I would very much like to.
use static yii\helpers\ArrayHelper\*;

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anton Spirin, 2019-08-27
@cloudz

1. You initiate the data load after the component mounts, but assign the state before.
2. In your case, you do not need to store data in the component's state at all.
3. By changing the filtering / sorting parameters, in a good way, a new request should occur with the appropriate parameters.

J
justadumb, 2019-08-27
@justadumb

why do you even put data from the store into the container state? pass directly the value from the store

I
Ivan Koryukov, 2017-11-17
@MadridianFox

No. And I do not understand your desire. A static method differs from a simple function in that when the method is called, it is visible to which class it belongs. This is good, because you may have several classes that have static methods with the same name, for example BlogPostAR::find()and CommentAR::find().
The same applies to helpers. Several libraries may provide their own helpers for similar tasks.
And if you are too lazy to type the class name - ok, use as :

use yii\helpers\ArrayHelper as ah;
ah::toArray($obj);

but I don't recommend that either. After a month, you will forget what ah means and will have to be distracted by finding the appropriate use. Or even worse, another person will maintain your code.
It is much clearer when it is written in the code ArrayHelper::toArray(), because everyone who works with yii is used to it.

A
Alexander Taratin, 2017-11-17
@Taraflex

Purely for reference, do not use in real code

<?php
function getStatic($c){
    $r = [];
    foreach((new ReflectionClass($c))->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC) as $v){
        $r[$v->name] = $v->class.'::'.$v->name;
    }
    return $r;
}

class Test{
    static function hello(){
        echo "hello\n";
    }
    static function world($p){
        echo "world $p\n";
    }
}

extract(getStatic('Test'));

echo '<pre>';
$hello();
$world(42);
echo '</pre>';
    
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question