Posts

Showing posts with the label PHP

When we can use abstract class & interface? Why we are using abstract class & interface?

  When to use Abstract Class? An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes. An abstract class is also good if we want to declare non-public members.  In an interface, all methods must be public. If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods. When to use interface? If the functionality we are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while. Interfaces are also good when we want to have something

What is the difference between self and static in PHP?

The main differences is that static allows late static bindings. One of the most useful scenarios that I found was for creating Base classes for Singleton Classes: class A { // Base Class     protected static $name = '';     protected static function getName() {         return static::$name;     } } class B extends A {     protected static $name = 'MyCustomNameB'; } class C extends A {     protected static $name = 'MyCustomNameC'; } echo B::getName(); // MyCustomNameB echo C::getName(); // MyCustomNameC Using return static::$name in the Base class will return what was statically attached when it was extended. If you were to use return self::$name then B::getName()  would return an empty string as that is what is declared in the Base class.

Why we need interface in PHP?

An interface allows unrelated classes to implement the same set of methods, regardless of their positions in the class inheritance hierarchy. An interface enables you to model multiple inheritance because a class can implement more than one interface whereas it can extend only one class. Interfaces are 100% abstract classes – they have methods but the methods have no ‘guts’. Interfaces cannot be instantiated – they are a construct in OOP that allows you to inject ‘qualities’ into classes .. like abstract classes. Where an abstract class can have both empty and working/concrete methods, interface methods must all be shells – that is to say, it must be left to the class (using the interface) to flesh out the methods. Interfaces allow you to define/create a common structure for your classes – to set a standard for objects. Interfaces solves the problem of single inheritance – they allow you to inject ‘qualities’ from multiple sources. Interfaces provide a flexible base/root s

What is autoload in PHP?

Autoloading is the process of automatically loading PHP classes without explicitly loading them with the require() , require_once() , include() , or include_once() functions. It's necessary to name your class files exactly the same as your classes. As of PHP 7.2.0 the __autoload() function has been deprecated. Now it is recommended to use the spl_autoload_register() for that purpose instead.

What are magic methods in PHP?

PHP supports multiple magic methods, those methods can be identified by two underscore prefix(__).  These are special functions should be defined by the user but no need to call them explicitly. It will be called on an appropriate event occurrence. For example, class __construct() will be called while instantiating the class. PHP magic methods must be defined inside the class. Note : Declaring the constructor method private prevents external code from directly creating an object. This is handy for creating singleton classes that restrict the number of objects that can exist. PHP Magic Methods and Purposes Below are the magic methods invoked on creating Class Instance - __construct() -  The __construct() method is most commonly used magic method. Here you can do initialization you need when an object is created. You can define any number of arguments that will be passed when creating objects. __destruct()  - The __destruct() method is called when the object is destroyed

Traits in PHP

What are the Traits in Laravel? Traits are a simple group of methods that you want to include in another class. Why we use Traits? A Trait, like an abstract class, cannot be instantiated by itself. The trait is created to reduce the limitations of single inheritance in PHP by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

What is generator in PHP?

A generator is basically a normal function, but instead of returning a value it yields as many values as it needs to. It looks like a function but acts as an iterator. Generators use the  yield  keyword instead of return. Sample Example -  <?php function generator() { echo "Generator started"; for ($i = 0; $i < 5; ++$i) { yield $i; echo "Yielded $in"; } echo "Generator ended"; } foreach (generator() as $g);

Object Oriented Programming (OOPs) Concepts in PHP

Class -  Class is the template which contains class members. class members are properties and methods. Property - Variable declared within the class. Method - Function declared within the class. Object -  An object is the memory location of the class variables. We can access the class variables with the support of object. New -  By using this keyword we can allocate space in the new memory locations to load class content. Constructor -  The constructor is one the type of method which is having class name same as method name. By default, every class contains a default constructor used to load class constraints. In PHP we can use constructor in two ways - Using constructor keyword (default constructor) - __constructor(). Using class name - className(). Destructor -  Destructor is the special type of method, which can be executed at the time of destroying object of class. By using __destructor() we can create destructor. In PHP , the class objects w

Basic PHP Interview Questions?

1) What is PHP?  PHP is a server-side scripting language that is basically used for developing dynamic web pages.  2) What is the difference between echo( ) and print ( )? echo and print are more or less the same. They are both used to output data to the screen.The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters while print can take one argument. echo is marginally faster than print.  3) What is print_r( )? This is also an output function used in PHP, It is used to display the whole array in PHP.  4) How to include files in PHP? We can include a file using "include( )" or "require( )" function with file path as its parameter. 5) What is the difference between GET and POST? We can send small data using GET method but POST method can transfer a large amount of data and POST is secure method than GET method.  7) How to declare an array in P

Difference between unset() and unlink() in PHP

In PHP unlink() is a function for file system handling, unlink() is used to delete files (physical). Suppose you have uploaded a file and wants to delete this file through the coding then unlink() function is used to delete the file, Suppose you have an image with the name test.png and you wants to delete this image then use the <?php /****Condition*****/ /****Condition*****/ unlink(image path/test.png); /****Condition*****/ /****Condition*****/ ?> or <?php $xx = fopen('testing.html', 'a'); fwrite($xx, '<h1>Hello EWA!</h1>'); fclose($xx); unlink('testing.html'); ?> unset() is a function for variable management. It will make a variable undefined. Or we can say that unset() is used to null out the value of a given variable. OR Unset () is used to destroy a variable in PHP. In can be used to remove a single variable, multiple variables, or an element from an array. Example for unset() : <?php $value =

PHP isset() vs empty() vs is_null()

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results. isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions. isset() From PHP manual – isset(): isset — Determine if a variable is set and is not NULL In other words, it returns true only when the variable is not null. empty() From PHP Manual – empty(): empty — Determine whether a variable is empty In other words, it will return true if the variable is an empty string, false, is_null() From PHP Manual – is_null(): is_null — Finds whether a variable is NULL In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that i

Basic Terminology

What is scripting language? A scripting language is a programming language that supports scripts programs written for a special run time environment that can interpret (rather than compile) automates and executes the tasks that could be alternatively executed by human operator. What is static Web pages? A static web page is the web page that delivered to user exactly as stored in contrast to dynamic web pages which are generated by a web application. What is Dynamic Web Pages?   A server side dynamic web page is controlled by an application server processing server side scripting. What is web server? Web server is the software used to run web application, Main functionality of server is to handle the request from the client and send response to client. What is web browser? The web browser is the software used to open the web application from the web server. Web browser can also execute HTML, Java Script, CSS and other client side script. What is

Introduction To PHP

PHP stands for Highpertext Preprocessor. PHP is a widely used open-source scripting language. PHP is web technology implemented by Rasmus Lerdorf in 1995. PHP is implemented using C & Perl software. PHP is a server-side scripting language used in implementing dynamic web pages. PHP is maintained by Zend Organization. What is PHP? PHP is basically server-side scripting language which is basically used for developing dynamic web pages. Why PHP? PHP code can run on any platform like windows, mac, linux etc. PHP is computable with all the web servers used today. PHP supports wide range of databases. PHP is easy to learn and it is freely available.