young call girls in Green Park🔝 9953056974 🔝 escort Service
Oops in php
1. B Y G O U R I S H A N K A R R P U J A R
OOPS in PHP
2. Introduction
It Provides Modular Structure for your application
It Makes Easy to maintain Existing Code.
Here we will be creating Class file & Index file.
Class file will be filled with Class, Objects, Functions.
Where as Index file Just Shows the result of that class or
Function when it is called.
3. List of Data types
Booleans
Integers
Floating Point Numbers
Strings
Arrays
Objects
Resources – File Handle
Null
Call-backs
6. How to use a Class
$object = new stdClass;
$object->names = [‘BMW’, ‘Audi’, ‘Benz’, ‘Jeep’];
foreach($object->names as $name){
Echo $name . “<br>”;
}
7. Example
Student.php
Class Student{
public $name;
public $rollno;
}
Index.php
Require(‘student.php’);
$student = new Student;
$student->name = “Gourish”;
$student->rollno = “7”;
Echo $student->name .‘ has a roll no of ’. $student->rollno;
8. Using Method
Student.php
Class Student{
public $name;
public $rollno;
public function sentence(){
return $this->name .‘ has a roll no of ’. $this->rollno;
}
}
Index.php
Require(‘student.php’);
$student = new Student;
$student->name = “Gourish”;
$student->rollno = 7;
Echo $student->sentence();
9. Contructors
This is also called as magic method
It has 2 Underscores.
This will be constructed when a class is loaded.
Public function __Construct(){
echo “Constructed”;
}
10. Example
Student.php
Class Student{
public $name;
public $rollno;
public function __construct($name, $rollno){
$this->name = $name;
$this->rollno = $rollno;
}
public function sentence(){
return $this->name .‘ has a roll no of ’. $this->rollno;
}
}
Index.php
Require(‘student.php’);
$student = new Student(“Gourish”, 7);
Echo $student->sentence();
11. Inheritance (including)
Bird.php
Class Bird{
public $canFly;
public $legCount;
public function __construct($canFly, $legCount){
$this->canfly = $canFly;
$this->legCount = $legCount;
}
public function canFly(){
return $this->canFly;
}
public function getlegCount (){
return $this-> legCount;
}
}
16. Visibility
Three access / visibility modifiers introduced in PHP 5, which
affect the scope of access to class variables and functions:
public : public class variables and functions can be accessed from inside and
outside the class
protected : hides a variable or function from direct external class access +
protected members are available in subclasses
private : hides a variable or function from direct external class access +
protected members are hidden (NOT available) from all subclasses
An access modifier has to be provided for each class instance
variable
Static class variables and functions can be declared without an
access modifier → default is public
17. Penguin.php
Change objects in bird class to Protected.
Class Penguin extends Bird{
public function foo(){
echo $legCount(); // This is picking up from protected object
}
}
18. Dependency Injection
Till Now we have understood Inheritance
But we don’t know how to utilize to its full potential.
What is Dependency ?
Create 3 files.
Index.php
Chest.php
Lock.php
19. Chest.php
Class Chest{
protected $lock;
protected $isClosed;
public function __construct($lock) {
$this->lock = true;
}
public function close($lock = true) {
if ($lock === true){
$this->lock->lock(); }
$this->isClosed = true;
echo “Closed”;
}
public function open() {
if ($this->lock->isLocked()){
$this->lock ->unlock(); }
$this->isClosed = false;
echo “Open”;
}
public function isClosed(){
return $this->isClosed;
}
}
20. Lock.php
Class Lock {
protected $isLocked;
public function lock(){
$this->isLocked = true;
}
public fuction unlock() {
$this->isLocked = false;
}
public function isLocked(){
return $this->isLocked;
}
}
23. Database Page
Class Database {
public function query($sql){
// $this->pdo->prepare($sql)->execute();
echo $sql;
}
}
24. User.php
Class User {
protected $db;
public function __construct(Database $db){
$this->db = $db;
}
public function create(array $data){
$this->db->query(‘INSERT INTO ‘users’ … ’);
}
}
26. Interfaces
What is Interface ?
Blueprint for a class.
3 Methods of file representation
1. Itest.php
2. I_Test.php
3. TestInterface.php
27. Example 1
Collection.php
Class Collection {
protected $items = [];
public function add ($value){
$this->items[] = $value;
}
public function set($key, $value){
$this->items[‘$key’] = $value;
}
public function toJson(){
return json_encode($this->items);
}
}
33. Magic Methods
What is Magic Method ?
__construct()
__set()
__get()
__call()
__toString()
34. SET Method
Public function __set($key, $value) {
$this->set($key, $value);
}
Public function set($key, $value){
$this->items[$key] = $value;
}
Public function all(){
return $this->items;
}
Index.php
$c->align = ‘center’;
Echo ‘pre’, print_r($c ->all());
35. Get
Public function __get($value) {
Return $this-> get(,$value);
}
Public function get($key){
retutn array_key_exists ($key, $this->items) ? $this->items[$key] : null;
}
Index.php
$c->align = ‘center’;
Echo $c->get(‘align’);
Echo $c->align;
36. Call
Public function __call($func, $args) {
echo $func.’ has been called with arguments
‘.implode(‘, ’. $args);
}
Index.php
$c->align = ‘center’;
Echo $c->align(‘left’, ‘right’, ‘center’, ‘top’, ‘bottom’);
37. toString
Public function __toString(){
Return $this->jsonSerialize();
}
Public function jsonSerialize(){
return json_encode($this->items);
}
$c->add(‘foo’);
$c->add(‘bar’);
Echo $c;
38. Abstract
What is Abstract ?
It is an interface in which we can define default
implemetations.
39. Example
User.php
Abstract Class User{
public function userdetails(){
return ‘Gourishankar’;
}
abstract public function userLocation();
}
40. Address.php
Class Address extends User{
public function userAddress(){
return ‘Bengaluru’;
}
public function userLocation(){
return ‘Magadi Road’;
}
}