Skip to main content

Posts

Showing posts with the label PHP

PHP Tutorials : Autoload PHP Classes

With some examples, this tutorial post will teach the PHP autoloader and namespace concepts. What is Autoloading in PHP? PHP autoload's primary function is to load PHP files into the application context. The import statement is the default method to include PHP file into context. However, we may avoid the issue by using the PHP autoload capability to autoload essential PHP files, which keeps your code clean and composable. Why do we need auto loading? When creating object-oriented applications, many developers create one PHP source file per class declaration. One of the major inconveniences is having to start each script with a huge list of required includes. Autloading allows us to include an arbitrary number of PHP files in the application context if they aren't already included by other include statements.  PHP Autoloading Methods PHP uses two functions; one is the magic method spl_autoload_register  and the other is   __autoload . Although __autoload has been

Doctrine 2 Tutorial: Installation and Configuration

In this tutorial, we will look at how to install Doctrine2 in your PHP project. This post will make use of Doctrine2's Composer tool. Installation First, create a directory for your project. mkdir zainabed cd zainabed Now, create a composer.json file. vi composer.json Then, add the following repository information to it . { "require" : { "doctrine/orm" : "*" } } You are now ready to use Composer to install Doctrine, but first you must install Composer on your machine. curl -sS https://getcomposer.org/installer | php To install Doctrine2, run the following command. php composer.phar install Note: Composer generates a "autoload.php" file to help in the autoloading of all PHP classes in the Doctrine2 ORM project. Configuration First, create a configuration file, configuration.php  for Doctrine2 and include  autoload.php  inside it. <?php // configuration.php // In

Twig Tutorials: Install and Configure

In this tutorial, we'll look at how to install Twig within your PHP project and thereafter configure it so that we can create and use a Twig template inside our PHP web application. Later on, we'll see a simple Twig template example that displays a "Welcome to Twig template" message. Let's take it one step at a time. Install The Twig template can be installed using Composer, Git, or PEAR. In this tutorial, we will use Composer to install Twig. To do so, we'll need to make a "composer.json" file. { "require" : { "twig/twig" : "1.*" } } From the console, run the following command . $ php composer.phar install This command will install Twig library. Configuration To use the Twig template, we should first configure it. To achieve this, we will write an index.php file which will configure the Twig autoloader and generate the Twig environment. We will render the Twig temp

Drupal 8 Tutorials : Custom Module

Custom Module development in Drupal 8 is very simple. It may looks very difficult for developer who has Drupal 7 experience or nothing at all. This tutorial will show easy steps and helps you to create custom module within few minutes. Note: Content or example of this tutorial may change according to Drupal 8 version and its release. This tutorial is divided into three small sections. In first section of this tutorial we will see what directory structure we need to create for a custom module. In second section we will see what type files we need to create. And in last section we will see what code we need to write in those files and configure them in Drupal 8. 1 Directory Structure All custom module in Drupal 8 reside under “module” directory of Drupal 8 project. To create a custom module we will create a new directory named “document” under custom folder. Following is directory structure. 2 Configuration files Secon

How To Watermark A Photo Using PHP

Hello Friends, This blog post will help you to create a watermark image using PHP. But first let’s get introduce yourself with watermarking. What is Watermark? Watermark is a marker which is embedded inside image (other media). It is use to identify the ownership. Why we need Watermarking? Watermarking helps to maintain integrity   and avoid unauthorized use for images (other media source also). For example let’s suppose you are have image hosting website and you provide unique and beautiful images to user. If you want only authorize user should download your images then you need to provide image preview with watermarking and original image to authorized user only so that unauthorized user cannot misuse it. Now let look at some coding stuff. First example shows watermarking images with text, normally we use copyright text. And second example show watermarking with watermark image. Watermarking using text // set water mark text $watermark = 'wate

AJAX - How To Retrieve JSON Response from PHP script

Hello Friends, This is my first tutorial post. Today I'll explain you how to retrieve JSON response via Ajax call. First we build a PHP script to send response in JSON format, then we will gather this response from an Ajax call through jQuery script. Let’s first create a PHP script  Create a PHP script, I’ll name it as “json_response.php” And add following code in it json_response.php <?php //set header for JSON response header("Content-Type: application/json"); //render array in JSON format echo json_encode(array('status' => 200, 'message' => 'Response from PHP script in JSON format.')); This is a basic example to send JSON response. PHP script will send response with status code and message. Now create a HTML file to fetch JSON response and add it in header tag. Create a HTML file, I’ll name it as “json_response.html” And add following code. json_response.html <html>          

Subscribe for latest tutorial updates

* indicates required