forked from chetanupare/php-programs-for-beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-object.php
More file actions
45 lines (34 loc) · 1.45 KB
/
php-object.php
File metadata and controls
45 lines (34 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!--Classes & Objects are two main aspects of oops
class is template for objects & objects is instance of Classes
when objects are created, they tooks all the properties & behavior from Classes
Each object will have different values for the properties
Lets assume we have a class named car.
car can have properties like model,color,year etc.
when such objects created as (Tata,BMW,Audi) they took properties of class car
but each objects will have different property values
if we create a __construct() function , PHP will automatically call this function when you create and object from a class
Examples:
-->
<?php
class Car {
public $color; //variables for properties
public $model;
public $year;
public function __construct($color,$model,$year) //function to declare properties for class
{
$this -> color = $color;
$this -> model = $model;
$this -> year = $year;
}
public function message()
{
return "My Car is a " . $this->color ." ".$this->model." ".$this->year." !"; // Function to return properties
}
}
$myCar = new Car("black","Tata","2002");
echo $myCar -> message();
echo "<br>";
$myCar = new Car("Red","Audi","2089");
echo $myCar -> message();
echo "<br>";
?>