2. 2
Agenda
o Access specifiers or modifiers
o Properties and Method
o get & set object’s Properties or Method
o $this Keyword
o Instanceof
o Static Properties & Methods
o Code
3. Access specifiers
or modifiers
protected
public private
access specifiers or modifiers are reserved keyword that is used to set the accessibility
of the classes, methods and other members of the class.
the property or
method can be
accessed from
everywhere.
the property or
method can be
accessed within the
class and by classes
derived from that
class.
the property or
method can ONLY be
accessed within the
class
5. 5
call properties to the variables inside a class.
Properties can accept values like strings, integers, and booleans (true/false
values), like any other variable
Properties
<?php
class Car {
public $comp;
public $color = ‘red';
public $isBoolean = true;
}
?>
add some properties to the Car class
Access
specifiers/modifiers
Assign value to
propertie
properti name/variable
6. 6
The classes most often contain functions. A function inside a class is called
a method.
Method
add method to the Car class
<?php
class Car {
public $comp;
public $color = ‘red';
public $isBoolean = true;
public function ShowText()
{
echo “Hello World”;
}
}
?>
Access specifiers/modifiers
Function keyword
User define function_name
7. 7
get & set
object’s Properties or Method
get or set a property, we write the object name, and then dash greater than (->), and then the property
name
[->] = Object Operator
<?php
class Car {
public $comp;
public $color = 'beige';
public $hasSunRoof = true;
public function hello()
{
return "beep";
}
}
?>
<?php
$bmw = new Car ();
$mercedes = new Car ();
echo $bmw -> hello();
echo $mercedes -> hello();
?>
8. 8
The $this keyword refers to the current object,
and is only available inside methods.
$this Keyword
9. 9
$this Keyword
example
<?php
class Fruit {
public $name;
}
$apple = new Fruit();
?>
change the value of the $name property,
There are two ways:
o Inside the class
o Outside the class
10. 10
$this Keyword
example
<?php
class Fruit {
public $name;
function set_name($name) {
$this->name = $name;
}
}
$apple = new Fruit();
$apple->set_name("Apple");
echo $apple->name;
?>
<?php
class Fruit {
public $name;
}
$apple = new Fruit();
$apple->name = "Apple";
echo $apple->name;
?>
Outside the class Inside the class ($this)
11. Code
11
<?php
class Fruit {
public $name;
public $color;
public $weight;
function set_name($n) {
$this->name = $n;
}
protected function set_color($n) {
$this->color = $n;
}
private function set_weight($n) {
$this->weight = $n;
}
}
$mango = new Fruit();
$mango->set_name('Mango'); // OK
$mango->set_color('Yellow'); // ERROR
$mango->set_weight('300'); // ERROR
?>
<?php
class Fruit {
public $name;
protected $color;
private $weight;
}
$mango = new Fruit();
$mango->name = 'Mango'; // OK
$mango->color = 'Yellow'; // ERROR
$mango->weight = '300'; // ERROR
?>
12. 12
instanceof
<?php
$apple = new Fruit();
var_dump($apple instanceof Fruit);
?>
Output:
bool(true)
can use the instanceof keyword to check if an object belongs to a
specific class:
13. 13
Static keyword
Static Properties & Methods
Static properties can be called directly - without creating
an instance of a class.
access a static property from outside class, we need to use
scope resolution operator (::) along with name of class
to use static inside any method of the same class, use self
keyword for accessing instance properties.
14. 14
Static Properties & Methods
example
<?php
class pi {
public static $value=3.14159;
public static function staticValue() {
return self::$value;
}
}
echo $pi::staticValue();
?>
public static $value=3.14159;
public static function staticValue() {
return self::$value;
}
Static Properties
Static Methods
16. Problem to Solve
16
o Write a simple PHP class which displays the following string
o Write a simple PHP class which displays an introductory message like "Hello All, I am Scott", where
"Scott" is an argument value of the method within the class
o Write a PHP class that calculates the factorial of an integer
o Write a PHP Calculator class which will accept two values as arguments, then add them, subtract them,
multiply them together, or divide them on request. Go to the editor
For example :
$mycalc = new MyCalculator();
echo $mycalc-> add(); // Displays 18
echo $mycalc-> multiply(); // Displays 72
o Print the average of three numbers creating a class named 'Average’ having a method to
calculate and print the average.
o Write a program to print the area and perimeter of a triangle by creating a class named
'Triangle’.