
$x after executing $x = 3 + "15%" + "$25" ? // 2- array_walk - array_walk($array, function($dog) { echo $dog->bark(); }); // // $dogs_bark $dogs_bark = function($dog) { echo $dog->bark(); } array_walk($array, $dogs_bark); class Dog { public function bark() { echo 'woof'; } } $dogs_bark = function($dog) use (&$collar) { // if ($collar == 'fitsWell'){ echo $dog->bark(); // 'woof' } else { echo 'no bark'; // ($collar) } }; $dog = new Dog; $collar = 'fitsWell'; // $dogs_bark($dog); // 'woof' $collar = 'tight'; $dogs_bark($dog); // 'no bark' array_walk($array, $calback) , which, like other similar functions, allows you to bypass the set of variables passed to it and process them in a specific way. This function bypasses the $array and at each iteration calls an anonymous function ( $callback ), passing to it only the value of the current element and its key. Therefore, the use of the $ collar variable without closure and the use construction will fail. Of course, we can use the global , but this will lead to the senseless cluttering of the global variable namespace, which is needed only in this particular context.Closure class: bind() and bindTo() , which can be used to bind new objects to the closure. For example: Closure::bindTo($newthis, $newscope); $this variable will become a reference to $newthis in the object context. Let's change the $dogs_bark function so that it uses the $this variable, and then bind it to the $dog object. // , $dogs_bark = function() { echo $this->sound; // sound - $this }; $new_dog = new Dog(); // $new_dog $new_closure = $dogs_bark->bindTo($new_dog); $new_closure(); // $sound $this is a fairly powerful feature. In particular, we can assign a closure to a property of an object, essentially turning it into a method of this object. $dog = new Dog(); $dog->closure = $dogs_bark; $dog->closure(); global keyword is used. Give an example when its use is appropriate and when not.global . In many ways, the use of global variables is a spit in the face of all the best practices of object-oriented programming. It leads to the emergence of excessive interdependence between classes, complicates the separation of logic, leads to pollution of the global namespace by variables that are used in one particular context and are not needed in others. class Dog { function bark() { global $sounds; return $sounds->bark(); } } Dog class on the $sounds global variable. Of course, there are cases when it is justified (suppose there is a single set of sounds in the system that never changes at all), but it is much better to explicitly transfer $sounds object of the Dog class in the constructor, and store and use it within this instance: class Dog { protected $sounds; function __construct($sounds) { $this->sounds = $sounds; } public function bark() { return $this->getSounds()->bark(); } public function getSounds() { return $this->sounds; } } Dog , defined in the application model Dog_Pound . Without a namespace, the full class name would look like this: class Dog_Pound_Model_Dogs { // , function getDogs(); } namespace dog_pound\model; // class Dogs { // "Dogs" dog_pound\model function getDogs(); } $dogs = new Dogs; // , // .. dog_pound space, you can create an instance of the Dogs class like this: $dogs = new model\Dogs // "model" dog_pound namespace this\new\namespace; // // Dogs dog_pound\model use dog_pound\model\Dogs; $dogs = new Dogs; trait Movement { public function topSpeed() { $this->speed = 100; echo "Running at 100 %!" . PHP_EOL; } public function stop() { $this->speed = 0; echo "Stopped moving!" . PHP_EOL; } } trait Speak { public function makeSound(){ echo $this->sound . PHP_EOL; } } class Dog { use Movement, Speak; // Dog protected $sound; function __construct() { $this->sound = 'bark'; } } $dog = new Dog(); $dog->topSpeed(); // Movement $dog->stop(); // Movement $dog->makeSound(); // Speak php://input and $_POST are interconnected and how to get access to the php: // input stream?$_POST is a superglobal array representing the analyzed and formatted request body sent to the server by the post method. POST /php-hiring-guide/php-post.php HTTP/1.1 Host: toptal.com Referer: http:///toptal.php/php-hiring-guide/php.php Content-Type: application/x-www-form-urlencoded Content-Length: 63 article_name=PHP Hiring Guide&tag_line=You are hired!&action=Submit $input = file_get_contents("php://input"); $_ , and give them a definition. Tell them about their relationship with the $GLOBALS variable.$_POST is an associative array containing key-value pairs sent to the server by the post method.$_GET is an associative array containing key-value pairs sent to the server using the get method.$_REQUEST - the union of key-value pairs from $ _POST and $ _GET.$_SERVER - represents the web server parameters related to the execution of the program.$_ENV - contains values ​​related to the server (host) and its configuration.$_SESSION is an associative array that stores the values ​​of session variables between page transitions and application launches.$_COOKIE - provides access to variables stored in cookies on the client.$_FILES is a special array for input data received when sending files to the server using the post method.$GLOBALS superglobal variable is a relative of the global . The $GLOBALS array stores all variables available in the global scope, including superglobal arrays. For example, access to $_ENV can be accessed like this: $GLOBALS['_ENV'];__get , __set , __isset , __unset , __cal l, and __callStatic . When, how and why should they be used (or not used)?private or protected , or not at all in the object. // 'whiskers' Dog, // __get: function __get($name) { if ($name == 'whiskers') { // whiskersService if (! isset($this->whiskersService)) { $this->whiskersService = new Whiskers($this); } // 'whiskers' whiskersService return $this->whiskersService->load(); } } $hairs = $dog->whiskers; __get method to intercept links to properties that look like public ones, we are able to hide the implementation details of these properties.__set method __set applied in the same way: function __set($name, $value) { if ($name == 'whiskers') { if ($value instanceOf Whisker) { $this->whiskersService->setData($value); return $this->whiskersService->getData(); } else { throw new WhiskerException("That's not a whisker"); return false; } } } 'whiskers' will look like this: $dog->whiskers = $hairs; __set() method with passing the property name ( 'whiskers' ) as the first parameter and the right side of the assignment operator as the second.__isset and __unset complete a quartet of methods for overloading properties. Each of them takes only one parameter - the name of the property, and is called when performing the isset() and unset() operations on this property. function __isset($name) { if ($name == 'whiskers') { return (bool) is_object($this->whiskersService) && !empty($this->whiskersService->getData()); } } function __unset($name) { if ($name == 'whiskers' && is_object($this->whiskersService)) { // $this->whiskersService->reset(); } } __call and __callStatic , perform a similar function, allowing you to implement method overloading . They allow us to determine how a class and its instances respond to attempts to invoke undefined, protected, or private methods.__call is called when a member is accessed in the context of an object, and __callStatic when a static member is accessed. As arguments, they both take the name $name method being called and the $args array with the parameters passed to the $name method.__call will lead to the fact that when accessing any "invisible" method, the whiskersService will be whiskersService : public function __call($method, $args) { return $this->whiskersService->load(); } __callStatic method works in the same way and takes the same arguments, but it is called when accessing the “invisible” method not in the context of an object, but in a static context. If we define this method, we will be able to handle calls like ClassName::notVisibleMethod() . public function __callStatic($method, $args) { if (!is_object(static::$whiskersService)) { static::$whiskersService = new Whiskers(__CLASS__); } return static::$whiskersService->load(); } $hairs = Dog::whiskers(); whiskers from the outside world, making it the only object available in this way. Clients of the methods and properties defined by us know nothing about the base class of whiskersService and how Dog stores its data in general.SplDoublyLinkedList is a doubly linked list. Each node of such a list stores a link to the previous one and to the node next to it. Imagine that you are in line at the bank and you can only see the person in front of you and behind you. This is an analogy of the relationship between the elements in SplDoublyLinkedList . Inserting an item into the list corresponds to the situation when someone got into the queue, and you suddenly forgot who was standing in front of you (and this someone forgot about you). .SplQueue SplStack SplDoublyLinkedList . , , ( IT_MODE_LIFO – Last In First Out – , ; IT_MODE_FIFO – First In First Out – , ), , . , SplQueue enqueue() dequeue() push() pop() SplStack .SplHeap – , , . , compare() , .SplMaxHeap SplMinHeap — SplHeap . SplMaxHeap compare() , , SplMinHea p – .SplPriorityQueue – , SplHeap , SplHeap priority (), .SplFixedArray – , . , , , , SplFixedArray ( ).SplObjectStorage – , , . .$x $x = 3 + "15%" + "$25" ?$x = 3 + "15%" + "$25" "15%" 15, "$25" — . 18 (3 + 15 + 0).static . , , self . Give an example.static ( static:: ) , , , . , « » , .. static . class Dog { static $whoami = ''; static $sound = ''; function makeSounds() { echo self::makeSound() . ', '; echo static::makeSound() . PHP_EOL; } function makeSound() { echo static::$whoami . ' ' . static::$sound; } } Dog::makeSounds(); class Puppy extends Dog { static $whoami = ''; static $sound = ''; function makeSound(){ echo static::$whoami . ' '; } } Puppy::makeSounds(); makeSounds() Dog .makeSounds() self::makeSound() . self:: , ( Dog ). self::makeSound() makeSound() Dog . , Puppy::makeSounds(), .. Puppy makeSounds() , makeSounds() Dog . , Puppy::makeSounds() self::makeSound() , « ».makeSounds() Dog static::makeSound() . static:: , self:: . static:: , ( , Puppy ). , static::makeSound() makeSound() Puppy . Puppy::makeSounds() static::makeSound() « ».ArrayAccess ArrayObject ?offsetGet , offsetSet , offsetExists , offsetUnset .ArrayAccess . ArrayObject , , [], : $dogs['sound'] , offsetGet(' ') , : $dogs->offsetGet('sound') .yield send ?foreach ), Generator , , , Iterator . , , . , , , :rewind() ;return – . yield (, yield $dog ). return , – .yield , . send (, $generator->send(-1); ). function dog_generator() { foreach (range(0, 2) as $value) { // $dog = DogHelper::getDogFromDataSource($value); // input send $dog $input = (yield $dog); if ($input == -1) { return; // } } } yield , . // $generator = dog_generator(); foreach ($generator as $dog) { // $dog - yield echo $dog . PHP_EOL; // if ($dog == 'Terrier') { // yield $generator->send(-1); echo ' , '; } } , ['-', ''] ). , register_globals .yield ), try/catch finally , APC Cache OpCache ( Zend Optimizer). (['-', ''][1] ''), (, '6e5d4'[0] «6»).Source: https://habr.com/ru/post/230805/
All Articles