What is OOPs?
OOPs (object oriented programming system) concept is use to make powerful, robust and secure programming instructions.
With any language reference there are only three basic object oriented prog concept.
- Encapsulation and data abstraction
- Inheritence
- Polymorphism
Advantages of OOPs
- Real world programming
- Ability to simulate real world event much more effectively.
- Reusable of code
- Information hiding
- Programmers are able to reach their goals faster.
Object and Class in PHP
What is Class:
- Class is a blueprint of related data members(proerties/variable) and methods(function).
- Classes are the fundamental construct behind oops.
- Class is a self contained independent collection of variables and functions, which work together to perform one or more specific related task.
Note: Variables within a class are called properties and functions are called methods.
How to define a class
Class always start with “class” keyword. After this write class name without parentheses.
Syntax
class demo
{
code to be executed
}
What is Objects
- Object is the instance of the class.
- Once a class has been defined, objects can be created from the class through “new” keyword.
- class methods and properties can directly be accessed through this object instance.
- Connector(Dot operator->) symbol used to connect objects to their properties or methods.
Note: You can not access any property of the class without their object>
Syntax
$obj= new demo();
This Keyword in PHP
To access or change a class method or property within the class itself,it’s necessary to prefix the corresponding method or property name with “$this”which refers to this class.Access current class properties inside current class method
Constructor and Destructor in PHP
Constructor In PHP
If a class name and function name will be similar in that case function is known as constructor.Constructor is special type of method because its name is similar to class name.Constructor automatically calls when object will be initializing.
Predefine Constructor
Php introduce a new functionality to define a constructor i.e __construct().By using this function we can define a constructor. it is known
as predefined constructor.Its better than user defined constructor because if we change class name then user.defined constructor treated as normal method.
Note: if predefined constructor and user defined constructor, both define in the same class, then predefined constructor work properly while user
defined constructor treated as normal method.
Destructor in PHP
The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.
Note : _ _destruct() is used to define destructor.
Encapsulation in PHP
Encapsulation is a concept of wrapping up or binding up related data members and methods
in a single module known as encapsulation
And hiding the essential internal property of that module known as data abstraction.
Inheritance in PHP
Inheritance is a mechanism of extending an existing class by inheriting a class we create a new class with all functionality of that existing class, and we can add new members to the new class.
When we inherit one class from another we say that inherited class is a subclass and the class who has inherit is called parent class.
We declare a new class with additional keyword extends.
Note : Php only supports multilevel inheritance.
Polymorphism in PHP
This word is can from Greek word poly and morphism.
Poly means “many” and morphism means property which help us to assign more than one property.
=> Overloading Same method name with different signature, since PHP doesn’t support method overloading concept
=> Overriding When same methods defined in parents and child class with same signature i.e know as method overriding
Public Private Protected PHP
Access Modifier allows you to alter the visibility of any class member(properties and method).
In php 5 there are three scopes for class members.
- Public
- Protected and
- Private
Public Access modifier
Public access modifier is open to use and access inside the class definition as well as outside the class definition.
Protected access modifier
Protected is only accessible within the class in which it is defined and its parent or inherited classes.
Private access modifier
Private is only accessible within the class that defines it.( it can’t be access outside the class means in inherited class).
Final keyword in PHP
The final keyword prevents child classes from overriding a method by prefixing the definition with final. it means if we define a method with final then it prevent us to override the method.
If the class itself is being defined final then it can’t be extended.
It means when we define a class with final then it will not allow to define it’s child class.
Abstract class
Abstraction is a way of hiding information . in abstraction there should be at least one method
which must be declared but not defined.
The class that inherit this abstract class need to define that method.
There must be a abstract keyword that must be return before this class for it to be abstract class.
this class cannot be instantiated . only the class that implement the methods of abstract class can be instantiated.
There can be more than one methods that can left undefined.
Interface
The class that is fully abstract is called interface.
any class that implement this interface must use implements keyword and all the methods that are declared in the class must be defined here. otherwise this class also need to be defined as abstract.
multiple inheritance is possible only in the case of interface.
