OOPs

 abstract.php

 

<?php

abstract class A{
    abstract function abc();
    // function abc();
    // cotain 1 abstract meethod : must implement this method.
    // either class -> abstract class
    // or declare the function

    // function abc();
    // non-abstract method: must contain body.
    function xyz(){
        echo "this is xyz";
    }
}
class B extends A{
    function mno(){
        echo "This is child calss: mno()";
    }
    function abc(){
        echo "This is child calss: mno()";
    }
}

$obj= new B();
$obj->abc();
echo "<br>";
$obj->mno();

echo "<br>";
$obj->xyz();

?>

 

 Access specifier.php


<?php

class Student{
    public $a=10;
    private $b=20;
    protected $c=30;

    function xyz(){
        echo $this->a;
        echo "<br>";
        echo $this->b;
        echo "<br>";
        echo $this->c;
        echo "<br>";
    }

}

class Demo extends Student{
    function xyz1(){
        echo $this->a;
        echo "<br>";
        echo $this->b;
        echo "<br>";
        echo $this->c;
        echo "<br>";
    }
}

class Temp extends Demo{
    function xyz2(){
        echo $this->a;
        echo "<br>";
        echo $this->b;
        echo "<br>";
        echo $this->c;
        echo "<br>";
    }
}

$obj = new Temp();
$obj->xyz();
$obj->xyz1();
$obj->xyz2();
echo "<br>";
echo $obj->a;
echo "<br>";
//echo  $obj->b; // cannot access protected property
echo "<br>";
//echo $obj->c; //cannot access protected property

?>


Function overload.php

<?php

class Student{
    function sum($a,$b,$c){
        echo $a+$b+$c;
    }
    function sum($a,$b){
        echo $a+$b;
    }
}

$obj = new Student();
$obj->sum(2,10,22);

?>


Function override.php

<?php

class A{
    function abc(){
echo "This is parent calss";
    }
}
class B extends A{
    function abc(){
        echo "This is child calss";
    }
}

$obj= new B();
$obj->abc();
?>


Generator function task 2.php

<?php
function nums() {
    for ($i = 5; $i > 0; $i--) {
        //get a value from the caller
        $cmd = (yield $i);
               
        if($cmd == 'stop')
        return;//exit the function
    }    
}

$gen = nums();
foreach($gen as $v)
{
    if($v == 1)//we are satisfied
        $gen->send('stop');
   
    echo "<br>$v";
}

//Output

?>


Generator.php

<?php  

function abc(){
    for($i=1; $i<=5;$i++){
        yield $i;
    }
}

$obj=abc();
// print_r($obj);
// Generator class r obj

forEach($obj as $val){
    echo $val;
    
}
?>


Generator fun task 1.php

<?php  
function abc(){

    for($i=5; $i>=1;$i--){
        yield $i;
    }
}

$obj=abc();

// print_r($obj);
// Generator class r obj

$val2=0;
$c=0;
echo "<br>Reverse Order Print: <br>";
forEach($obj as $val){
    echo $val;
    $val2+=$val;   
    $c+=1;
}

echo "<br>Average is: ".$val2/$c;
?>



Function overloading using call.php

<?php
// PHP program to explain function
// overloading in PHP
 
// Creating a class of type shape
class shape {
     
    // __call is magic function which accepts
    // function name and arguments
    function __call($name_of_function, $arguments) {
             
        // It will match the function name
        if($name_of_function == 'area') {
             
            switch (count($arguments)) {
                     
                // If there is only one argument
                // area of circle
                case 1:
                    return 3.14 * $arguments[0];
                         
                // IF two arguments then area is rectangle;
                case 2:
                    return $arguments[0]*$arguments[1];
            }
        }
    }
}
     
// Declaring a shape type object
$s = new Shape;
     
// Function call
echo($s->area(2));
echo "\n";
     
// calling area method for rectangle
echo ($s->area(4, 2));
?>

<!-- Function Overloading: Function overloading contains same function name and that function performs different task according to number of arguments. For example, find the area of certain shapes where radius are given then it should return area of circle if height and width are given then it should give area of rectangle and others. Like other OOP languages function overloading can not be done by native approach. In PHP function overloading is done with the help of magic function __call(). This function takes function name and arguments.  -->


Scalar.php

<?php

$a = '5'+'5';
echo $a;

?>


Constructor and Destructor in Php:

<?php

class Student{

    function __construct(){
    echo "This is constructor";
    }

    function std(){
        echo "Login";
    }
function __destruct(){
    echo "This is __destructor<br><br>";
    }
}
$i=1;
while($i<=10){
$obj = new Student();

$obj->std();
$i++;
}

?>


constructor.php

<?php

class Student{
function __construct(){
    echo "This is constructor";
    }
}
$obj = new Student();



?>


destructor.php

<?php

class Student{
function __destruct(){
    echo "This is __destructor";
    }
}
$obj = new Student();



?>


Encapsulation.php

<?php
class Person{
public $name, $age, $sage;
function __construct($name,$age){
    // echo "<br>Bulding done";
    // echo "<br>Name: ".$name;
    // echo "<br>age: ".$age;
    // echo "<br>";
    $this->name = $name;
    $this->age = $age;
    }

function setage($sage){
    // echo "<br>Reset age";
    // echo "<br> Age: ".$sage;
    $this->sage = $sage;
}
function display(){
    echo "<br><br> Display:";
    echo "<br> name: ".$this->name;
    echo "<br> Age: ".$this->age;
    return $this->sage;
}

}

$obj = new Person("Saif",5);
$obj->setage(22);
echo "<br> Set New Age: ".$obj->display();


?>


Interface.php

<?php

interface A{

    function a();
    // function c(){
    // echo "<br>This is C"    ;   
    // }
}

interface B{
    function b();
}

class C implements A,B{

function b(){
    echo "<br>This is B";
    }

function a(){
    echo "<br>This is A";
    }
function c(){
    echo "<br>This is C"    ;   
    }

}


$obj = new C();
$obj->a();
$obj->b();
$obj->c();

?>


multilevel.php

<?php

class A{
    // parent class
    function mno(){
        echo "<br>This is parent class<br>";
    }
}

class B extends A{
    // child class
    function abc(){
        echo "<br>this is 2nd parent/ 1st child class<br>";
    }

}

class C extends B{
    // child class
    function xyz(){
        echo "<br>this is least child class<br>";
    }

}

$obj = new C();
$obj->mno();
$obj->abc();

$obj->xyz();

// :: scope resolution operator



?>


multiple.php

<?php

class A{

    function a{
    echo "This is A"    ;   
    }
}

class B{
function b{
    echo "This is B"    ;   
    }
}

class C extends A,B{
function c{
    echo "This is C"    ;   
    }
}

class C extends A extends B{
function c{
    echo "This is C"    ;   
    }
}
$obj = new C();


?>


Polymorphism.php

<?php

class Shape
{
    function draw(){

    }
}

class c extends Shape{
    function draw(){
        echo "<br>Circle draw";
    }
}

class t extends Shape{
    function draw(){
        echo "<br>triangle draw";
    }
}

class e extends Shape{
    function draw(){
        echo "<br>Ellipse draw";   
    }
}

$obj = array();
$obj[0]=new c();
$obj[1] = new t();
$obj[2]= new e();
for($i=0;$i<3;$i++)
{
    $obj[$i]->draw();
}
?>


Singlelevel.php

<?php

class A{
    // parent class
    function mno(){
        echo "<br>This is parent class<br>";
    }
    
}

class B extends A{
    // child class
    function abc(){
        echo "<br>this is child class<br>";
    }

}

$obj = new B();

$obj->abc();
$obj->mno();

// :: scope resolution operator



?>


Trait.php-

<?php

trait A{

    function a{
    echo "This is A"    ;   
    }
}

trait B{
function b{
    echo "This is B"    ;   
    }
}

class C {
    use A,B;
function c{
    echo "This is C"    ;   
    }
}

class C extends A extends B{
function c{
    echo "This is C"    ;   
    }
}
$obj = new C();


?>


const_parent.php

<?php

class A{
    
    function __construct(){
    echo "This is constructor A";
    }
}

class B extends A{
    

function __construct(){
    parent::__construct();
    echo "<br>This is constructor B";
    }
}

$obj = new B();

// $obj->abc();
// $obj->mno();

// :: scope resolution operator



?>



Comments

Popular posts from this blog

[[ ROYAL CAR ]] CHANGE PASSWORD - DYNAMIC BANNER - MULTIPLE IMAGE - LOGIN LOGOUT BACK BUTTON MIDDLEWARE STOP - MAIL DIRECTLY WITH FEEDBACK WITH SAVE IN SQL DB - ADMIN REPLY EXISTING MAILS - DYNAMICALLY CSS CHANGE

Sahari

Linux Terminal