i
PHP Variables
PHP Data Types
PHP Echo & Print
PHP Strings
PHP Numbers
PHP Constants
PHP Operators
PHP if...else...elseif Statements
Switch Statement
PHP Loops
PHP Arrays
Superglobals
PHP Coding Standards
PHP Form Handling
PHP Form Validation
PHP URLs Validation
PHP Form Required Validation
Complete Form Example
PHP File Functions Open/Read
PHP File Create/Write
PHP File Upload
PHP Cookies Handling
PHP Session Handling
PHP filter_var() Function
PHP Validation Filters
PHP Sanitization Filters
Using Filters
Filters Advanced
JSON
PHP Date and Time
MySQL Database
MySQL Connect
MySQL Commands-Creating a Table
MySQL Commands-Inserting The data
MySQL Commands-Prepared Statement
MySQL Commands-Selecting The Data
MySQL Commands-Where and Order By
MySQL Commands-Deleting And Updating The Data
PHP-OOP Introduction
PHP-Classes/Objects
PHP-Constructor/Destructor
PHP-Access Modifiers
PHP-Inheritance
PHP-Inheritance and Protected Access Modifier
PHP-Overriding Inherited Methods
PHP-Final keyword
PHP-Abstract Classes
PHP-Constants
PHP-Traits
PHP-Static Methods and Properties
Introduction to Functions
Defining A function
Returning Values From A Function
Dynamic Function Calls
Variable Scope
Understanding Arguments Or Parameters
Testing For A Function Existence
Returning Multiple Values From A Function
Making practical Use By Building Code Libraries For Code Re-usability
Using Include() And Require()
In Object oriented programming, a class is considered to be a template for the objects and object is known as the instance of a class. For example, if Car is a class then Audi, BMW, Mercedes are the objects of this class. The car has its own attributes like name, speed, mileage etc. and all the cars will have these attributes.
So, when we create a class, we define some attributes that will be common to the objects of this class. When an object is created, all the objects will have some value for all the attributes of their class.
You can define a class using class keyword and give the name of the class and mention all the properties and methods inside curly braces {}.
Syntax: class class_name { //properties and methods }
We can create methods for setting the properties and getting the properties as below:
class Car {
public $name; //properties
public $color;
function set_name($name) { //method
$this -> name = $name; }
function get_name($name) {
return $this -> name; }
}
?>
$this keyword refers to the current object of a class and is only used inside the methods of class.
Classes need objects as they would not have any meaning otherwise. You can create multiple objects from a class, and it will have all the properties and methods of the class but a different value for those properties. You can create an object using new keyword.
class Cars {
public $name;
public $color;
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;
}
}
$Audi = new Cars();
$Audi->set_name('Audi');
$Audi->set_color('Black');
echo "Name: " . $Audi->get_name();
echo "
";
echo "Color: " . $Audi->get_color();
?>
You can also change the property of an object outside the class by using below PHP code:
class Cars {
public $color;
}
$BMW = new Cars();
$BMW->color = "White";
?>
Don't miss out!