Static Methods and Properties:
Static Methods and Properties is very useful feature in PHP and in this post i am going to tell you about Static Methods and Properties in PHP with example.
If you declare any class property or methods as static then you don't need to create object of class to access that means it can be accessible without creating object of class. You can access directly from class with the help of scope resolution operator (::).
You can use static keyword to define static methods and properties.
class myClass {
static public $variable1 = 5;
static public $variable2 = 2;
static public function getSum() {
$sum=self::$variable1+self::$variable2;
print "Sum of two variables is " . $sum;
}
}
echo myClass::$variable1;
myClass::getSum();
If you extends the parent class in child class and want to access parent class property then you can use parent keyword.parent::$variable1
Why use static methods and properties?
Now the question is why you will use static methods and properties in your application.
While working with large OOP based project you will have to use many classes and as i told you static methods aren't associated with an instance so from any class you can access the properties of any other class by their class name and you won't need to pass elements of different class manually through each class because static elements can be accessible from anywhere in your application.
Encapsulation :
Wrapping some data in a single unit is called Encapsulation. Encapsulation is used to save data or information in an object from another it means encapsulation is mainly used for protection purposes.In the technology era, it is essential to maintain your privacy. So for security reasons, Sometimes we make a private method.
The private method means it can be accessed within the same class. Outside, the Class can't access the private method of other classes. That's why encapsulation is known as data hiding is the main advantage for encapsulation.
The second advantage of encapsulation is you can make the class read-only or write only by providing a setter or getter method.
Polymorphism:
Polymorphism is a design pattern in which classes have different functionality while sharing a common
interface
.
You can define an interface in PHP using
interface
or abstract
classes.interface Shape { public function area(); }
now let's suppose we have different classes, like class circle, class Rectangle,
- If a class has only one method as abstract, then that class must be an abstract class.
- Declaring a class abstract only means that you don't allow it to be instantiated on its own.
class Circle implements Shape { public function area() { return $this -> radius * $this -> radius * pi(); } }
class Rectangle implements Shape { public function area() { return $this -> width * $this -> height; } }so all the classes have same function name with different behavious, it's Polymorphism.
Multilevel and Multiple Inheritance in PHP:
PHP OOP does not allow multiple inheritances, it allows only multilevel inheritance.
In simple words, a subclass can not extend more than one superclass.
But PHP allows hierarchical inheritance, Hierarchical inheritance means a child can get property of their parent and parent can get the property of grandparent, so in this way, a child can get also some property of their grandparent.
But PHP allows hierarchical inheritance, Hierarchical inheritance means a child can get property of their parent and parent can get the property of grandparent, so in this way, a child can get also some property of their grandparent.
Interface in PHP :
An Interface does not contain any functionality, it only specify set of methods which classes must implement.
You can create interface in php oop by using interface keyword.
Properties of Interfaces :
1 - The classes that implement the interfaces must define all the methods that they inherit from the interfaces, including all the parameters.
2 - All methods declared in an interface must be public; this is the nature of an interface. otherwise, it will generate an error
3 - The class implementing the interface must use the exact same method signatures as are defined in the interface.
4 - Interfaces can be extended like classes using the extends operator.
5 - If we extend the interface all the methods of the interface must be implemented in the child class.
6 - We cannot define a variable in an interface.
Like Below,
You can implement your interface in classes by using the implements keyword.
<?php
interface A {
public function Compute();
}
interface B extends A {
public function Divide();
}
class C implements B {
public function Divide() {
echo “division of 10/2 is” . 10/2;
}
public function Compute() {
echo “multiplication of 2*3 is” . $a*$b;
}
}
You can implement your interface in classes by using the implements keyword.
If you will use other than public visibility then it will generate an error.
Abstract Classes & Methods :
An abstract class is a class that has at least one abstract method. Abstract methods can only have names and arguments, and no other code.
Thus, we cannot create objects of abstract classes. Instead, we need to create child classes that add the code into the bodies of the methods and use these child classes to create objects.
Features of Abstract Classes :
- only has function name & parameter no body of code,
- Can not create the instance of the abstract class,- If a class has only one method as abstract, then that class must be an abstract class.
- Declaring a class abstract only means that you don't allow it to be instantiated on its own.
- The child class which extends an abstract class must define all the methods of the abstract class. it is required. only need to define the methods which are abstract we can leave the other methods.
- You can declare your abstract method in child class with the same visibility or less restricted visibility.
- The signatures of the methods must match,
- The signatures of the methods must match,
optional parameter given in the child class will be accepted.
Comments
Post a Comment