OOP(Object Oriented Programming ) used on Laravel.
PHP.
Object Oriented Programming.
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();
?>
<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>