Learn How To Use OOP On Laravel PHP Framework In Laravel

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

OOP used on Laravel is PHP framework

OOP_used_on_Laravel

OOP(Object Oriented Programming ) used on Laravel.

PHP.

  1. PHP is a server-side scripting language.
  2. PHP used for web development.

Laravel.
  1. Laravel is free ,Open source PHP web framework.
  2. Laravel is MVC(model-view-controller) architectural pattern.
 

Object Oriented Programming.

  1. Advance features and major concept was introduced from version PHP5
  2. Reduce complex risk in web application.

PHP | OOP - Classes and Objects
  1. Classes and objects are core of OOP.
  2. PHP itself is OOP.
  3. In Laravel, almost everything works on classes and objects
There are some concept of OOP's
  1. Class and Object
  2. Polymorphism
  3. Abstraction
  4. Encapsulation
  5. Constructor
  6. Destructor
  7. Inheritance
Example

Classes cant do anything without objects! We can create multiple objects from a class. Each object has all the properties and functions defined in the class, but they will have different property values.Objects of a class is created using the new keyword.
In the example below, $bike1 and $bike2 are objects of the class bike:

 

<?php
class Bike {// class
    // Properties
    public $name;
    public $color;

    // Methods
    function set_name($name) {
        $this->name = $name;
    }
    function get_name() {
        return $this->name;
    }

    function set_color($color) {
        $this->color = $color;
    }
    function get_color() {
        return $this->color;
    }
}

$bike1 = new Bike();//object 1
$bike2 = new Bike();//object 2
$black = new Bike();//object 3
$gray = new Bike();//object 4

$bike1->set_name('BIKE 1');
$bike2->set_name('BIKE 2');

$black->set_color('Black');
$gray->set_color('Gray');

echo $bike1->get_name();
echo "<br>";
echo $bike2->get_name();
echo "<br>";

echo $black->get_color();
echo "<br>";
echo $gray->get_color();
?>

Complete code of OOP's used on Laravel (PHP framework):
 
<html>
<head>
    <title>OOP used on Laravel (PHP framework)</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container ">
    <div class="text-center">
        <h1>OOP used on Laravel (PHP framework)</h1>
    </div>
    <br>
    <br>
    <div class="well">
        <?php
        class Bike {// class
            // Properties
            public $name;
            public $color;

            // Methods
            function set_name($name) {
                $this->name = $name;
            }
            function get_name() {
                return $this->name;
            }

            function set_color($color) {
                $this->color = $color;
            }
            function get_color() {
                return $this->color;
            }
        }

        $bike1 = new Bike();//object 1
        $bike2 = new Bike();//object 2
        $black = new Bike();//object 3
        $gray = new Bike();//object 4

        $bike1->set_name('BIKE 1');
        $bike2->set_name('BIKE 2');

        $black->set_color('Black');
        $gray->set_color('Gray');

        echo $bike1->get_name();
        echo "<br>";
        echo $bike2->get_name();
        echo "<br>";

        echo $black->get_color();
        echo "<br>";
        echo $gray->get_color();
        ?>

    </div>
</div>
</body>
</html>

Related Post