What is an API Contract & Why You Need One with Code Examples

An API contract is a formal agreement between the provider and consumer of an API. It outlines each party's expectations and responsibilities and specifies the rules and requirements for using the API. The rules include the format and structure of requests and responses, the supported methods and parameters, error handling, and other technical details. Learn what an API Contract is and why you need one. We will also discuss the types of API contracts, design, and OpenAPI/Swagger contracts. Let’s begin! Article Highlights An API contract is a document that defines the rules and requirements for interacting with an API. Contract-first API development means designing and developing an API by creating a contract or specification before writing any code. OpenAPI/Swagger is a widely used API contract that allows you to define and document RESTful APIs. API contract design involves creating a clear and comprehensive contract. And it defines the API's endpoints,…

read more

Use the Memento Design Pattern | PHP Code Examples in 2023

Using the Memento Pattern in PHP The memento pattern is a behavioral design pattern which lets you save and restore an object to its previous state without compromising its privacy. Article Highlights The memento pattern delegates creating a snapshot to the original object while keeping the state in a memento object. Benefit -   Doesn’t violate the data encapsulation of the original objects. Con -        Creating memento objects too often can consume much memory. Memento Design Pattern PHP Code Example <?php interface Originator { public function createSnapshot(); } // The originator class class WhiteBoard implements Originator { private $fontSize; private $fontFamily; private $canvasSize; private $canvasColor; private $output; //More fields... function __construct($fontSize, $fontFamily, $canvasSize, $canvasColor, $output) { $this->fontSize = $fontSize; $this->fontFamily = $fontFamily; $this->canvasSize = $canvasSize; $this->canvasColor = $canvasColor; $this->output = $output; } function setFontSize($fontSize) { $this->fontSize = $fontSize; } function setFontFamily($fontFamily) { $this->fontFamily = $fontFamily; } function…

read more

19 Best Beginner PHP Web Development Books to Learn Code

Welcome to our article about the best beginner PHP web development books! If you're looking to expand your knowledge and improve your skills in web development, you've come to the right place. We have collected a list of the top books for beginner PHP web developers. In this article, we've compiled a list of the top books that cover everything from HTML and CSS to PHP and MySQL. These books will provide valuable insights, tips, and techniques to help you create better, more efficient, and more user-friendly websites for any beginner web developer. So, without further ado, let's dive in and explore the best books for web development! Table of Contents Books on Web Development Concepts & Theories PHP Programming Books PHP & MySQL Books HTML, CSS & Javascript Books Beginner Web Development Books on Theory & Concepts Book Title & AuthorPublish DateKey ConceptsPage LengthChapter CountDon’t Make Me Think, Revisited:…

read more

Use the Command Design Pattern | PHP Code Examples in 2023

Using the Command Pattern in PHP The command pattern is a behavioral design pattern that encapsulates a request into an object (command) that knows all about the receiving entity of this request. It allows you to parameterize objects with different requests, schedules or log requests and supports undoable operations. Article Highlights The command pattern decouples the invoker of action from the receiver by encapsulating the request for action as a separate object (command object). Benefit - You can separate one application layer from another and use command objects to allow communications between the two. Con - The code becomes complicated with all the new command classes in the application Command Design Pattern PHP Code Example <?php interface Command { public function execute(); } class LightOnCommand implements Command { private $light; //Expects reference to the Light object public function __construct($light) { $this->light = $light; } public function execute() { $this->light->switchOn(); }…

read more

5 PHP mysqli_query Code Examples to Learn SQL in 2023

Using PHP mysqli_query The following are the common steps while using the PHP mysqli_query() function to execute queries on a SQL database server. Create a connection to the server using mysqli_connect() Specify a SQL query string. Call mysqli_query() with the connection object and SQL string. Consume the return value. ?php $servername = "localhost"; $username = "fuelingphp"; $password = "fuelingphp"; $database = "fuelingphp"; // Creates a connection $connection = mysqli_connect($servername, $username, $password, $database); // Checks if the connection has been established. if (!$connection) { die("Connection failed: " . mysqli_connect_error()); } // SQL query to create a new table $sql = "CREATE TABLE articles ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(30) NOT NULL, category VARCHAR(30) NOT NULL )"; //Executes query if (mysqli_query($connection, $sql)) { echo "New table articles has been created"; } else { echo "Error: " . $sql . " : " . mysqli_error($connection); } // SQL query to…

read more

How to Post JSON Payload in cURL | PHP Code Examples (2023)

Sending and receiving data in JSON format is common when working with APIs. In PHP, you can use the cURL library to send HTTP requests, including sending JSON data in a POST request. In this article, we will explore how to POST JSON payload using the PHP cURL library in a step-by-step guide. We will also learn what JSON is and what JSON payload is. Post JSON Payload in CURL PHP Code Example <?php // create the JSON payload as a PHP array $data = array( 'name' => 'John', 'age' => 45, 'isMarried' => true, 'hobbies' => array('reading', 'traveling', 'cooking'), 'address' => array( 'street' => '123 Main St', 'city' => 'Tokyo', 'state' => 'CA', 'zip' => '12345' ) ); // convert the PHP array to JSON format $jsonPayload = json_encode($data); // set the CURL options $ch = curl_init('https://example.com/api'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER,…

read more

Using the State Design Pattern | PHP Code Examples in 2023

Using the State Pattern in PHP The state pattern is a behavioral design pattern that allows an object to alter its behavior when its internal state changes. It appears as if the object changed its class. The first statement makes sense, given that the state diagram is based on the idea of states and corresponding behavioral changes when the state machine transitions between various states. The second statement is more apparent from a client’s perspective because when the object changes its behavior, it appears that it is an instance of some other class. The client doesn’t see all the state transitions that happen within, thus creating a perception that the object changes its class in the runtime. Article Highlights Defines classes for all the states, encapsulating their behaviors. The client delegates actions to the state objects. Benefit - Creates a flexible and maintainable state machine. Cons -  Adds as many…

read more

Using the Observer Design Pattern | PHP Code Examples in 2023

Using the Observer Pattern in PHP The observer pattern is a behavioral design pattern that defines a one-to-many relationship between objects such that when the state of an object (publisher) changes, the other objects (subscribers) are notified. Article Highlights The observer pattern has a publisher object which updates all its subscribers when its state changes. Benefits -  A change in the state of one entity is propagated to other entities. Con - Sends updates in a random order which can be detrimental if observers have shared data dependency where sequence matters. Observer Design Pattern PHP Code Example <?php interface Publisher { public function registerDisplay($display); public function removeDisplay($display); public function updateDisplays(); } interface Observer { public function update($sportsNews, $politicsNews, $weatherNews); } /* This class represents the publisher class now. NewsData implements publisher and also has an array of references to the subscriber objects. */ class NewsData implements Publisher { private $displays;…

read more

Store foreach Loop Value in Array: 4 PHP Code Examples (2023)

How to Store Loop Values in a PHP Array There are 3 steps to storing loop values in PHP arrays. You need to instantiate a PHP variable with an empty array. Then, you will run your requirements with a loop like foreach. Finally, you can check for any needed conditions and add a value to the newly instantiated array. Check out the remaining article for examples. Are you needing to store values from a loop in a new array? This article will break down some common scenarios that I have experienced throughout my programming career. Storing Foreach Loop Values In Array PHP Code Example <?php // Define an indexed array $numbers = [5, 4, 2, 6, 10]; // Define an empty array to store the resulting values $results = []; // Loop through the array using foreach loop // store any 2, 6, 10 values in the new array to…

read more

Use the Template Method Design Pattern | PHP Code Examples in 2023

Using the Template Method Pattern in PHP The template method pattern is a behavioral design pattern that defines the steps of an algorithm and allows sub-classes to provide an implementation for one or more steps without affecting the algorithm structure. Article Highlights The template method defines the steps or skeleton of an algorithm and allows sub-classes to override one or more of these steps. Benefit - Helps remove code duplication, making the system less rigid to changes. Con - May limit flexibility in tinkering with the steps of an algorithm. Template Design Pattern PHP Code Example <?php abstract class IceTea { public function prepareIceTea() { $this->addBoilWater(); $this->addTeaBag(); $this->addSugar(); $this->brewTea(); $this->addColdWater(); $this->addIce(); } public function addBoilWater() { echo "Adding boil water"."\n"; } public abstract function addTeaBag(); public function addSugar() { echo "Adding sugar"."\n"; } public function brewTea() { echo "Brewing tea"."\n"; } public function addColdWater() { echo "Adding cold water"."\n"; }…

read more

Page 3 of 9
1 2 3 4 5 9