Posts For Tag: PHP
Myths About Asynchronous PHP: It Is Not Truly Asynchronous
03 Mar 2021
Recently I had a lot of discussions about performance in PHP. Even though we have PHP 8, JIT, and all other improvements, people still continue complaining about PHP. That it is a language only for the request-response circle. That PHP...
READ MOREDriftPHP: Quick Start
08 May 2020
Idea and Architecture Let’s imagine that you are building a high-performance PHP application. Speaking of me previously I had two options: either use Symfony or ReactPHP. And to tell the truth, both solutions have their one pros and cons. With...
READ MOREReactPHP Internals: Event Loop Timers
01 May 2020
For many of you, ReactPHP and especially its EventLoop component looks like magic. We know that it handles concurrency, but the way it works inside is a sort of black box. In these tutorials I want to dig into it,...
READ MOREInterview with Marc Morera: About DriftPHP
14 Feb 2020
Recently I interviewed Marc Morera. He is an author of DriftPHP - a new asynchronous non-blocking framework on top of Symfony Components and ReactPHP. We discussed a new framework and how it became possible to make Symfony asynchronous and run...
READ MOREIntroducing PHP-Watcher
18 Oct 2019
The problem I like PHP for its simplicity: you code something, refresh the page in the browser and you can see your changes. There is no need to wait, to compile something, just refresh the page and you are ready...
READ MORELive Reloading PHP Applications With Nodemon
16 Sep 2019
When building a traditional web application in PHP we don’t care about reloading it. We make some changes in the source code, save it, then make the request in the browser or some other client and we can see these...
READ MOREBuilding a RESTful API Using ReactPHP: JWT Authentication
22 Apr 2019
Previously we have used Basic HTTP Authentication to protect our RESTful API. This authentication method is pretty simple, but in most cases, it can be used only in the internal network with server-to-server communication. For example, we can’t store Basic...
READ MOREBuilding a RESTful API Using ReactPHP: Basic Authentication
20 Apr 2019
In the previous article, we have created a RESTful API on top of ReactPHP HTTP server. Now we want to protect our API and add authentication. When it comes to securing a RESTful API things became interesting because a truly...
READ MOREBuilding a RESTful API Using ReactPHP and MySQL
18 Feb 2019
Today we will be looking at creating a RESTful API using ReactPHP, MySQL and nikic/FastRoute. Let’s look at the API we want to build and what it can do. Application We are going to build the API that: Handles CRUD...
READ MOREHow To Speed Up The Code Review
29 Dec 2018
How does the code review process look? You submit a request, then receive feedback, make some fixes, push them, then receive approval and the request is being merged. Sounds nice, right? But sometimes the review process can become really painful....
READ MOREManaging Concurrency: From Promises to Coroutines
26 Oct 2018
What does concurrency mean? To put it simply, concurrency means the execution of multiple tasks over a period of time. PHP runs in a single thread, which means that at any given moment there is only one bit of PHP...
READ MOREFast Web Scraping With ReactPHP: Download All Images From a Website
31 Aug 2018
Web scraping relies on the HTML structure of the page, and thus cannot be completely stable. When HTML structure changes the scraper may become broken. Keep this in mind when reading this article. At the moment when you are reading...
READ MOREPHP Roundtable 76: Concurrency, Generators & Coroutines - Oh My!
26 Jul 2018
Today was my first experience of being a guest on the podcast. Really excited about it! We had a very interesting discussion about asynchronous PHP, generators, and coroutines.
READ MOREFast Web Scraping With ReactPHP. Part 3: Using Proxy
20 Jun 2018
Web scraping relies on the HTML structure of the page, and thus cannot be completely stable. When HTML structure changes the scraper may become broken. Keep this in mind when reading this article. At the moment when you are reading...
READ MORESending Email Asynchronously With ReactPHP Child Processes
04 May 2018
Introduction In PHP the most of libraries and native functions are blocking and thus they block an event-loop. For example, each time we make a database query with PDO, or check a file with file_exists() our asynchronous application is being...
READ MOREFast Web Scraping With ReactPHP. Part 2: Throttling Requests
19 Mar 2018
Web scraping relies on the HTML structure of the page, and thus cannot be completely stable. When HTML structure changes the scraper may become broken. Keep this in mind when reading this article. At the moment when you are reading...
READ MOREUsing Router With ReactPHP Http Component
13 Mar 2018
Router defines the way your application responds to a client request to a specific endpoint which is defined by URI (or path) and a specific HTTP request method (GET, POST, etc.). With ReactPHP Http component we can create an asynchronous...
READ MOREWorking With FileSystem In ReactPHP
27 Feb 2018
I/O operations in the filesystem are often very slow, compared with CPU calculations. In an asynchronous PHP application this means that every time we access the filesystem even with a simple fopen() call, the event loop is being blocked. All...
READ MOREAmp Promises: From Generators To Coroutines
15 Feb 2018
Generators Generators become available in PHP since version 5.5.0. The main idea is to provide a simple way to create iterators but without creating a class that implements Iterator interface. A generator function looks like a normal function, except that...
READ MOREFast Web Scraping With ReactPHP
12 Feb 2018
Video Web scraping relies on the HTML structure of the page, and thus cannot be completely stable. When HTML structure changes the scraper may become broken. Keep this in mind when reading this article. At the moment when you are...
READ MOREAsynchronous PHP: Why?
02 Feb 2018
Video What is ReactPHP Asynchronous programming is on demand today. Especially in web-development where responsiveness of the application plays a huge role. No one wants to waste their time and to wait for a freezing application, while you are performing...
READ MOREDoes Static Factory Violate Open/Closed Principle?
25 Jan 2018
Consider an application that provides some statistics reports. Reports are present in different formats: JSON for API, HTML for viewing in a browser and pdf for printing on the paper. It has StatisticsController that receives a required format from the...
READ MOREIntroduction To Amp Event Loop
19 Jan 2018
Event Loop All asynchronous magic would be impossible without Even loop. It is the core of any asynchronous application. We register events and handlers for them. When an event is fired the event loop triggers an appropriate handler. This allows...
READ MOREManaging ReactPHP Promises
16 Jan 2018
Asynchronous application is always a composition of independently executing things. In concurrency, we are dealing with a lot of different things at once. You can compare it with I\O driver in your OS (mouse, keyboard, display). They all are managed...
READ MORETest Coverage: Integration Between CodeClimate and Travis CI
11 Jan 2018
When you maintain an open-source project it is considered a good practice to have a high test coverage, so the community can feel safe about using your code in their projects. There are some services that can analyze your code...
READ MOREReactPHP HTTP Server Middleware
20 Dec 2017
What is middleware? What exactly is middleware? In real application when the request comes to the server it has to go through the different request handlers. For example, it could be authentication, validation, ACL, logging, caching and so on. Consider...
READ MOREReactPHP PromiseStream: From Promise To Stream And Vice Versa
07 Dec 2017
ReactPHP PromiseStream Component is a link between promise-land and stream-land From Stream To Promise One of the patterns that are used to deal with streams is spooling: we need the entire resource data available before we start processing it. One...
READ MOREBuilding ReactPHP Memcached Client: Unit-Testing Promises
20 Nov 2017
This is the last article from the series about building from scratch a streaming Memcached PHP client for ReactPHP ecosystem. The library is already released and published, you can find it on GitHub. In the previous article, we have completely...
READ MOREBuilding ReactPHP Memcached Client: Emitting Events
03 Nov 2017
This is the third from the series about building from scratch a streaming Memcached PHP client for ReactPHP ecosystem. The library is already released and published, you can find it on GitHub. In the previous article, we have faced with...
READ MOREBuilding ReactPHP Memcached Client: Errors And Connection Handling
14 Oct 2017
This is the second article from the series about building from scratch a streaming Memcached PHP client for ReactPHP ecosystem. The library is already released and published, you can find it on GitHub. In the previous article, we have created...
READ MOREBuilding ReactPHP Memcached Client: Making Requests And Handling Responses
09 Oct 2017
This is the first article from the series about building from scratch a streaming Memcached PHP client for ReactPHP ecosystem. The library is already released and published, you can find it on GitHub. Before writing any code we should think...
READ MOREUnderstanding ReactPHP Event Loop Ticks
25 Sep 2017
What Is Tick? Tick is one loop iteration where every callback in the queues has been executed synchronously and in order. ReactPHP event loop implementation has a method to schedule a callback to be invoked on a future iteration of...
READ MOREPromise-Based Cache With ReactPHP
15 Sep 2017
In the previous article, we have already touched caching (when caching DNS records). It is an asynchronous promise-based Cache Component. The idea behind this component is to provide a promise-based CacheInterface and instead of waiting for a result to be...
READ MOREResolving DNS Asynchronously With ReactPHP
03 Sep 2017
Basic Usage It is always much more convenient to use domain names instead of IPs addresses. ReactPHP DNS Component provides this lookup feature for you. To start using it first you should create a resolver via factory React\Dns\Resolver\Factory. Its create()...
READ MORECancelling ReactPHP Promises With Timers
22 Aug 2017
The Problem At first, let’s refresh in memory what is Promise. A promise represents a result of an asynchronous operation. You can add fulfillment and error handlers to a promise object and they will be invoked once this operation has...
READ MOREManaging Child Processes With ReactPHP
07 Aug 2017
ReactPHP Child Process Component enables an access to Operating System functionalities by running any system command inside a child process. We have access to that child process input stream and can listen to its output stream. For example, we can...
READ MOREMaking Asynchronous HTTP Requests With ReactPHP
26 Jul 2017
The Problem We need to perform batches of HTTP requests. For example, we need to download several video files. We can start downloading them one by one, but it will take a lot of time since we need to wait...
READ MOREBuilding Video Streaming Server with ReactPHP
17 Jul 2017
In this article, we will build a simple asynchronous video streaming server with ReactPHP. ReactPHP is a set of independent components which allows you to create an asynchronous application in PHP. ReactPHP Http is a high-level component which provides a...
READ MORESingleton: Anti-Pattern Or Not
11 Jul 2017
Definition — “Do you know any Singleton jokes?” — “Just one.” Singleton is one of the simplest patterns to understand. The main goal of it is to limit the existence of only one instance of the class. The reason for...
READ MOREUDP/Datagram Sockets with ReactPHP
05 Jul 2017
Streams vs Datagrams When you send data through the sockets, there are situations when you really do not care if some packets are lost during the transition. For example, you are streaming a live video from your camera. The order...
READ MOREBuild A Simple Chat With ReactPHP Socket: Client
24 Jun 2017
Video In the previous article, we have created a simple chat server based on ReactPHP Socket component. We have used a telnet client to connect to this server, now it’s time to create our own PHP client, also based on...
READ MOREBuild A Simple Chat With ReactPHP Socket: Server
22 Jun 2017
Video In this article, we are going to build a simple chat server based on ReactPHP Socket Component. With this component, we can build simple async, streaming plaintext TCP/IP or a secure TLS socket server. Socket A socket is one...
READ MOREEvent-Driven PHP with ReactPHP: Promises
16 Jun 2017
Video Promises ReactPHP Promise Component The Basic Concepts A promise represents a value that is not yet known while a deferred represents work that is not yet finished. A promise is a placeholder for the initially unknown result of the...
READ MOREEvent-Driven PHP with ReactPHP: Streams
12 Jun 2017
Video Streams ReactPHP Stream Component In PHP streams represent a special resource type. The description given in php.net documentation: Streams are the way of generalizing file, network, data compression, and other operations which share a common set of functions and...
READ MOREEvent-Driven PHP with ReactPHP: Event Loop And Timers
06 Jun 2017
Video What is ReactPHP The Problem PHP was born in the 90s and was a very powerful tool for creating web pages. From its born it has a synchronous run-time, that means that we start execution of some function, and...
READ MOREWhen to use Factory instead of direct object construction
22 May 2017
Factory is an object responsible for creating other objects. It is often considered a good practice to move the process of object creation from the consumer’s code into the factory. Even more, some people say that you should avoid the...
READ MOREAntipattern Poltergeist
11 May 2017
The opposite of the God class is Poltergeist. It are some sort of useless class. The main characteristic of a poltergeist: it represents a piece of control logic for making things happen and then it disappears, no state, no data...
READ MOREDependency Injection Smells
04 May 2017
In the previous article, I have discussed the difference between constructor and setter injections. Now, it’s time to pay attention to some examples of how not to use Dependency Injection. Too many dependencies When a class has a lot of...
READ MOREDependency Injection: Constructor vs Setter
25 Apr 2017
In an object-oriented application, objects constantly interact with each other, either by calling methods and receiving information from another object, or changing the state of this object. In any case, objects often depend on each other. Consider this simple. Delivery...
READ MOREOOP Desing Principle: Dont Repeat Yourself
19 Apr 2017
The DRY principle is stated as Every piece of knowledge must have a single, unambiguous, authoritative representation within a system. It is always easy to copy and paste some code when you need it in some other place of your...
READ MOREKISS Your Application
13 Apr 2017
Another principle of object-oriented design is called KISS, that stands for Keep it short and simple. But there also some interpretations such as keep it simple, stupid!, keep it simple and stupid or keep it simple and straightforward. Sounds too...
READ MOREMaking Polymorph
09 Apr 2017
In object-oriented programming, polymorphism is one of the most important concepts. The general description of the word polymorphism in the programming community is the provision of a single interface to entities of different types. There are several types of polymorphism,...
READ MOREBuild Pinterest Bot with PHP: Parsing Pins
04 Apr 2017
Today we are going to create a simple Pinterest images parser. It will search for pins according to some keywords and then save them to disk. So, let’s start. For interacting with Pinterest in PHP we will Pinterest Bot for...
READ MOREPHP: How to override trait method and call it from the overridden one?
01 Apr 2017
Consider this example, when we have a trait and a class that uses this trait. But we want to override a trait’s method and also we want to call the initial trait’s method. How can we do it? We can’t...
READ MOREBuild Pinterest Bot With PHP: Followers
01 Apr 2017
Next step in promoting our blog on Pinterest is to follow popular accounts in our niche. Pinterest Bot for PHP library provides this functionality for us. I’m not going to cover installation and setup of the library here. You can...
READ MOREBuild Pinterest Bot with PHP: Comments, Likes And Repins
30 Mar 2017
Notice, that likes functionality has been disabled by Pinterest. In the previous articles, we have created a script that makes pins every five minutes and uses multiple accounts. But this is not enough, nobody will see your pins if you...
READ MOREBuild Pinterest Bot with PHP: Multiple Accounts and Proxy
28 Mar 2017
In the previous post, we have created a small script to automate pins creation on Pinterest. It uses only one your account, but what if you have several accounts and want to use them all. And of course, it is...
READ MOREOpen/Closed Principle
27 Mar 2017
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. Let’s decrypt this description a bit. Open means that it should be simple to change the behaviour of a particular entity. Closed means chaging behavior...
READ MOREBuild Pinterest Bot with PHP: Automate pinning
25 Mar 2017
Our goal is to create an account on Pinterest, that behaves as a real person. He creates pins, likes or dislikes pins of other users, writes comments, so, behaves as a real person. For example, we are going to promote...
READ MORETraits: Copy-Paste Behavior
20 Mar 2017
In PHP we don’t have multiple inheritance. Our child classes should always have only one parent class. Some people argue that this is a bad approach because they want to inherit from multiple classes to get access to some useful...
READ MOREInheritance: Families Of Related Types
18 Mar 2017
What’s the first thing that comes to your mind when you hear word inheritance? I have no doubt that it is code reuse. Right? Let’s forget about code reuse and start from scratch. In one of my previous posts, I...
READ MOREOOP Design Antipattern: God Object
11 Mar 2017
God Object means an object that does too much or knows too much. To be more detailed this situation occurs when an object has knowledge of many different large parts of the system, it has an influence on them, the...
READ MOREDon't Use Your Testing Tool As A Spell Checker
04 Mar 2017
Today, when I started refactoring tests for my PHP Pinterest Bot library, I’ve noticed one issue. According to coverage, reports I have 80% code coverage. I though that I’m writing a lot good tests. But then I noticed that most...
READ MOREReplace Conditionals With Composition And Polymorphism
25 Feb 2017
Conditionals are an integral part of any programming language. We use them every day, so why in OOP they are considered as a code smell? Conditioт becomes a code smell when we have to check an object’s type in order...
READ MOREAbstract Class VS Interface
05 Feb 2017
Abstract class Abstract class represents a new data type in your application. Classes define a blueprint for the objects, and we know how to use these objects of the certain class type. Just like we know how to use strings...
READ MORETesting: Mocks vs Stubs
29 Jan 2017
In testing, we often have to deal with a lot of different jargon: dummies, stubs, mocks, fakes. Stubs A stub is a generic term for any kind of pretend object used in place or a real one for testing purposes....
READ MOREThe Decision Between Inheritance And Composition
08 Jan 2017
There are two fundamental ways to establish relationships between class in object-oriented design: inheritance and composition. First of all, let’s determine that there is no better or worse principle in object-oriented design. But there is an appropriate design for the...
READ MORESymfony Console Component In Yii
27 Dec 2016
In Laravel 5 I enjoy its nice console output based on Symfony Console Component. Working with legacy code in Yii in console we have a very poor output. We can use echo and format the output ourselves. The better choice...
READ MOREPHP7: Exceptions And Errors Handling
24 Dec 2016
In the previous versions of PHP, there was no way to handle fatal errors in your code. Setting the global error handler with set_error_handler() function doesn’t help, the script execution will be halted. This happens because of the engine. Fatal...
READ MOREOOP Design Principle: The Principle of Least Knowledge
11 Dec 2016
In this article, I’m going to touch a problem known by many names, one of which is the Law of Demeter. But honestly speaking, it is not even a law, but a guideline. The rules promoted by this principle are:...
READ MOREOOP Design Principle: Tell Don't Ask
04 Dec 2016
Procedural code gets information then makes decisions. Oject-oriented code tells objects to do things. - Alec Sharp. Example of issues In PHP you can meet it very frequently and sometimes may consider that it is actually the right way to...
READ MOREUse Booleans In Method Params Carefully
28 Nov 2016
Today when I was refactoring my PHP Pinterest Bot, I’ve noticed that it has a logout method with a boolean param. So when you want to logout, you use something like this: <?php $bot->auth->logout(); But you can also type this:...
READ MOREPraying to SRP God
30 Oct 2016
According to DDD, we should write our code in that way, that it matches the language of the business. So it can be really readable. For example, an Order. We have orders, and we can pay them. So, the easiest...
READ MOREPHP Tumblr Downloader Part 2. Creating console application
28 Sep 2016
In the previous chapter, we have created a script, that can download all the photos from any Tumblr blog. But it is not very convenient to use. The path to a folder for saved photos and the blog itself are...
READ MOREPHP Tumblr Downloader Part 1. Saving photos from blogs
20 Sep 2016
Registering App in Tumblr I’m going to create a script to rip all images/videos from one’s blog from Tumblr. To interact with Tumblr I will use their official PHP client. The client itself requires API credentials: <?php $client = new...
READ MOREPHP Closure As Macro
12 Sep 2016
Macros When we talk about closures we often think about anonymous functions. Functions without name: <?php $heyFunc = function($name) { return "Hey, {$name}"; } echo $heyFunc('John'); If we take a context of a single web request, named functions exist for...
READ MORELaravel: Problems With Migrations And SQLite
02 Aug 2016
When testing with SQLite, I’ve met several problems with migrations. Cannot add a NOT NULL column with default value NULL When you have a migration that changes columns in a table: <?php public function up() { Schema::table('users', function (Blueprint $table){...
READ MORELaravel 5.0: use database and Eloquent for logging
30 Jul 2016
Out Of Date Warning This article was published on 30 Jul 2016, this means the content may be out of date or no longer relevant. You should verify that the technical information in this article is still current before relying...
READ MORELaravel: Cache Response With Middleware
25 Jul 2016
Imagine that we want to increase the speed of our site responses. Of course, we will use cache. We can cache requests to the database, we can cache views, but we can also cache the whole response. But the response...
READ MORELaravel middleware
20 Jul 2016
So, what really means middleware? HTTP middleware provide a convenient mechanism for filtering HTTP requests, that are sent into the application. They work like the layers of the onion. When the request comes into the application it has to go...
READ MORELaravel Request Lifecycle
14 Jul 2016
Lets dive into the magic of Laravel and the way it handles http requests. First of all, the entry point for all the requests is the public/index.php file. It is usually called the front controller. The web server (Apache or...
READ MOREWell written methods rule: one level of indentation
17 Jun 2016
What is the main purpose of creating a new method? To reduce the complexity of the code. We create a method to hide and forget some logic. Later we use methods and ignore their logic details. One of the signs...
READ MORETricky PHP
16 May 2016
I like PHP, it is an easy, flexible language. We type something, refresh our browser and see results. No compilation, everything works “out of the box”. But we should be very carefull with PHP, with it’s flexibility out of the...
READ MOREThree Rules to Respect Liskov Substitution Principle
02 May 2016
Program to an Interface, not implementation Let’s refresh a classic definition: objects in a program should be replaceable with instances of their subtypes without altering the correctness of the program. In the world of PHP it often means programming to...
READ MOREPHP: Interface and Abstract Class
19 Apr 2016
One of the most popular questions on the interview is “What is the difference in interfaces and abstract classes?”. So let’s see the difference. Interfaces Before getting into theory, let’s refresh in memory how an interface is defined: <?php interface...
READ MORESymfony Event Dispatcher
15 Apr 2016
The idea of event dispatcher is to allow communication between different parts of the application without coupling them. In this way, an application can be easily maintained and extended, and it does not have to know about the classes that...
READ MOREAbstraction in PHP: Define Type Not Class
20 Mar 2016
A trap of code reuse What is an abstraction in PHP? Everybody understands it, but it is sometimes difficult to explain. The first thing that comes to mind is “it is about code reuse”. And it is what we have...
READ MORE