Posts

Showing posts with the label Object Oriented Programming Concept in PHP

Object oriented programming concepts in PHP

Is it always necessary to create objects from class? No. An object is necessary to be created if the base class has non-static methods. But if the class has static methods, then objects don’t need to be created. You can call the class method directly in this case, using the class name. How much memory does a class occupy? Classes do not consume any memory. They are just a blueprint based on which objects are created. Now when objects are created, they actually initialize the class members and methods and therefore consume memory. Can you create an instance of an abstract class? No. Instances of an abstract class cannot be created because it does not have a complete implementation. However, instances of subclass inheriting the abstract class can be created. What is a final variable? A variable whose value does not change. It always refers to the same object by the property of non-transversity. What are the differences between error and exception? Exception - Exception can be recovered b

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 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

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