Hello, tell me the name of the following syntax, when the variable itself plays the role of a function:

$arr = array_filter(explode(' ', trim($str)), function($v) { return mb_strlen($v) > 1; }); 

And please answer the second question, is there a complete if if check, which can also be done through a variable, like a ternary, that is:

 $a = if($b = 0) { //присваиваСм $a = 0 return 0; } else { //присваиваСм $a = 1 return 1; } 
  • It seems the javascript label is superfluous here) - Stepan Kasyanenko
  • @StepanKasyanenko the thing is what I mean javascript too, just example code from php - Michael
  • Well, these are different languages, and what can be done in php cannot be done in js and vice versa. Therefore, please clarify your question. - Stepan Kasyanenko
  • in the first case, your variable is assigned the values ​​of the array_filer function. and the third filtering parameter is an anonymous function. - teran

2 answers 2

when the variable itself plays the role of a function

getters / setters
getters / setters

php

  class Animal { function __get($property) { //... } function __set($property, $value) { //... } } $cow = new Animal; $cow->weight = '1 ton'; // same as $cow->__set('weight', '1 ton') print $cow->weight; // same as print $cow->__get('weight'); 

js

 let obj = { conteiner: {}, get name() { return this.conteiner.name }, set name(_name) { let type = typeof _name if ('string' == type) { this.conteiner.name = _name console.log('_name строка'); } else if ('number' == type) { this.conteiner.name = ''+_name console.log('_name чисто'); } else { this.conteiner.name = null console.log('Π½Π΅Π²Π°Π»ΠΈΠ΄Π½Ρ‹ΠΉ _name'); } }, } obj.name = [] console.log(obj.name); obj.name = 7 console.log(obj.name); obj.name = 'qwa' console.log(obj.name); 

ternary operator

php

 // ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Ρ‚Π΅Ρ€Π½Π°Ρ€Π½ΠΎΠ³ΠΎ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; // ΠŸΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½Ρ‹ΠΉ Π²Ρ‹ΡˆΠ΅ ΠΊΠΎΠ΄ Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π΅Π½ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΌΡƒ Π±Π»ΠΎΠΊΡƒ с использованиСм if/else if (empty($_POST['action'])) { $action = 'default'; } else { $action = $_POST['action']; } 

js

 let $action; // ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Ρ‚Π΅Ρ€Π½Π°Ρ€Π½ΠΎΠ³ΠΎ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Π° $action = (true) ? true : false // ΠŸΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½Ρ‹ΠΉ Π²Ρ‹ΡˆΠ΅ ΠΊΠΎΠ΄ Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π΅Π½ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΌΡƒ Π±Π»ΠΎΠΊΡƒ с использованиСм if/else if (true) { $action = true } else { $action = false } 

UPD:

analog php __get in javascript

https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Proxy https://learn.javascript.ru/proxy

 let user = {}; let proxy = new Proxy(user, { get(target, prop) { console.log(`Π§Ρ‚Π΅Π½ΠΈΠ΅ ${prop}`); return target[prop]; }, set(target, prop, value) { console.log(`Π—Π°ΠΏΠΈΡΡŒ ${prop} ${value}`); target[prop] = value; return true; } }); proxy.firstName = "Ilya"; // запись proxy.firstName; // Ρ‡Ρ‚Π΅Π½ΠΈΠ΅ console.log(user.firstName); // Ilya 

    php :

    In the first example, the variable $arr takes the result of the function array_filter . In this case, it does not represent a function. $arr will be assigned to the filtered array. Call it assignment :

     $arr = array_filter(explode(' ', trim($str)), function($v) { return mb_strlen($v) > 1; }); 

    And it doesn't make any sense at all:

     $a = if($b = 0) { //присваиваСм $a = 0 return 0; } else { //присваиваСм $a = 1 return 1; } 

    The assignment through the ternary operator looks like this:

     $a = ($b == 0) ? 0 : 1; // сравнСниС $b ΠΈ присваиваниС $a // $a = 0 $a = ($b = 0) ? 0 : 1; // присваиваниС $b,сравнСниС ΠΈ присваиваниС $a // $a = 1 

    If we talk about " синтаксис, ΠΊΠΎΠ³Π΄Π° нСпосрСдствСнно пСрСмСнная ΠΈΠ³Ρ€Π°Π΅Ρ‚ Ρ€ΠΎΠ»ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ", this is called a function variable :

     function foo(int $param) { return $param + 1; } $foo = 'foo'; // Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ пСрСмСнная $foo Π²Ρ‹Π·ΠΎΠ²Π΅Ρ‚ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ foo() echo $foo(1); // 2 

    Also in the language there are anonymous functions :

     $a = function(int $param) { return $param + 1; }; var_dump($a(1)); // int(2)