Php Course – Worldwideasy – 05

php lesson 5. I will focus on Class, Construct, Object, getter, setters, and Inheritance. Finally, we can complete today’s php basic course.

Php Class

Class is collection of objects and functions.

This is simple mining for the class. We find a class in object-oriented programming. The use of this concept revolutionized the software development industry. This made it possible to create very fast software with a small amount of code. The specialty here is the ability to reuse code. Let’s see how to use it for php. The following example shows how to declare and execute a class.

<?php
class Book {
var $name;
var $author;
var $price;               //class assing
}
$book1 = new book;
$book1 = name = "Harry Potter";
$book1 = price = "1700";          //make objects
$book1 = author = "JK";

echo $book1 -> name;
echo $book1 -> price;
?>

This code output is Harry Potter 1700.

Constructor

In class-based object-oriented programming, a constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables

You can get a good idea using the following example about php constructor.

<?php
class Book{
var $name;
var $price;
var $author;
function__construct(){
echo"this is book<br>";
}
}
$book1 = new Book;
$book2 = new Book;
?>

This code output is is book two-time print.

Object and Functions

In computer programming, a function object is a construct that allows an object with the same syntax to be called or called a normal function. Active objects are often referred to as funksters.

This is the mining of the object and functions. now we can try to add it php. look below example.

<?php
class student {
var $name;
var $stream;
var $gpa;
 function__construct ($name,$stream,$gpa){
$this->name = $name;
$this->stream = $stream;
$this->gpa = $gpa;
}
function hasHonors(){
if ($this->gpa>2.5){
return "true";
}
else{
return "false";
}
}
}
$student1 = new student ("jane","Maths","3.2");
$student2 = new student ("mick","Biology","2.4");

echo $student1 -> hasHonors();
echo $student2 -> hasHonors();
?>

This code answer is truly false.

Getters and Setters

get create a function function getRating(){}

using if statement function setRating($rating)

Private can not be changed. The public can be change.

Inheritance

Object-oriented programming, inheritance, is the mechanism by which an object or class retains a similar function based on another object or class. Also defined as taking new classes from existing classes, such as the superclass or the basic class, and converting them into a hierarchy of classes.

see below you can get an idea for inheritance.

<?php
class chef{
function makechicken(){
echo "make chicken";
}
function make salad(){
echo "make salad";
}
function make bbq(){
echo "make bbq";
}
}
$italianchef = new italianchef;
echo $italiancheff->makechicken();
echo $italiancheff->makebbq();
?>

output is made chicken and makes bbq.

Now over the php basic course. you go to this link and answer the question and get PHP certificate. Get Certificate

Thank you for all!

More php ClickHere!

See you Next Lesson Soon!

Leave a Comment

Your email address will not be published. Required fields are marked *

Translate »