1) A class is a template for objects, and an object is an instance of class.
for example - car is class, bmw, mercedes is object
2) Define function in class called method,
define variable in class called properties.
three principle in OOPS
A) Encapsulation - Binding (or wrapping) code and data together into a single unit are known as encapsulation.
For example, a capsule, it is wrapped with different medicines.
via the use of "get" and "set" methods etc.(hide implementation detail)
for example -
<?php
class Animal
{
private $family;
private $food;
}
?>
B) Inheritance - via the use of extends keyword (inherit from parent to child)
C) Polymorphism - If one task is performed in different ways, it is known as polymorphism
for example -
i) draw shape like circle, triangle (draw task in different ways)
ii) speak something like a cat speaks meow, dog barks woof (speak task in different ways)
for polymorphism use implements keyword .
Abstraction - Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing
class car
{
public $color = 'red' //property
// method create
public function useofthis()
{
return $this->color; //using this keyword get property within same class
}
}
$bmw= new car(); //object create
echo $bmw->color // get property not add $ for property when calling
echo $bmw->useofthis() //get method
3) A constructor allows you to initialize an object's properties upon creation of the object.
function __construct($name) { //double underscore before construct word
$this->name = $name;
}
4) If you create a __destruct() function, PHP will automatically call this function at the end of the script.
5)
public - the property or method can be accessed from everywhere. This is default
protected - the property or method can be accessed within the class and by classes derived from that class
private - the property or method can ONLY be accessed within the class
6) Inheritance in OOP = When a class derives from another class.
-- child class has its all properties and method from parent class(main class) include its own property and method
7) We can access a constant from outside the class by using the class name followed by the scope resolution operator (::)
class Goodbye {
const LEAVING_MESSAGE = "Thank you for visiting W3Schools.com!";
}
echo Goodbye::LEAVING_MESSAGE;
8) Abstract classes and methods are when the parent class has a named method, but need its child class(es) to fill out the tasks.
9) Interfaces allow you to specify what methods a class should implement.
define by interface keyword
10) Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
11)Namespace -allow for better organization by grouping classes that work together to perform a task,
allow the same name to be used for more than one class
namespace ab;
No comments:
Post a Comment