Object Methods in PHP
Let's move on to methods now. Methods are essentially functions that each object can call. When writing code, the difference between methods and properties is that for methods, you must write parentheses at the end, and for properties, you don't.
Let's practice. Let's make a show
method,
which will output some text:
<?php
class User
{
public $name;
public $age;
public function show()
{
return '!!!';
}
}
?>
Let's create a user object:
<?php
$user = new User;
$user->name = 'john';
$user->age = 25;
?>
Let's call our method:
<?php
echo $user->show(); // will output '!!!'
?>
Make an Employee
class with a show
method.
Let this method output some text.