PHP & OOP: Methods of a class

This entry is part 7 of 11 in the series PHP & OOP

In object-oriented programming, the member functions of a class are also called methods of the class.

If you read any literature related to object-oriented programming, you will come across this word frequently.

Look at the following example:

<?php

class PopStar
{
    public $person;
    public $salary;
    public $album;

    public function setSalary()
    {
        $this->salary = 10000;
    }

    public function getSalary()
    {
        echo $this->salary;
    }
}

$star = new PopStar();
$star->setSalary();
echo "The pop star's salary is:";
echo "<br>";
$star->getSalary();

?>

There are 2 methods setSalary() and getSalary() in the above PopStar class.

From the code above, we can say that we are using the method setSalary() to set the salary of the pop star and method getSalary() to get the salary of the star.

PHP & OOP: Properties of a class

This entry is part 6 of 11 in the series PHP & OOP

Class member variables are called ‘properties‘.

When I first started learning OOP using C++, I thought this is odd.  It sounds abstract.

But look at the following declarations for the 2 class members and you might be able to understand.

<?php

class MovieStar
{
    public $person = 'Tom Cruise';
    public $salary = 10000;
    public $movie = 'Mission Impossible';
    public $no_of_movies = 20;
}

class PopStar
{
    public $person = 'Madonna';
    public $salary = 8000;
    public $album = 'Crazy For You';
    public $no_of_album = 12;
}

?>

From the class properties such as $movie and $album, we could guess what the 2 classes are about.

PHP: Static variable

This entry is part 8 of 54 in the series PHP Tutorial

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

Consider the following example:

<?php
function final_no()
{
    static $num = 0;
    echo $num;
    $num++;
    echo "<br>";
}

final_no();
final_no();
final_no();

?>
Try The Code

The output from the above code is 0, 1, 2.

The $num was declared once as a static variable and will be initialized on the first call to final_no().  Everytime final_no() is called, $num will not be re-initialized to 0.

PHP & OOP: $this variable

This entry is part 5 of 11 in the series PHP & OOP

In this example, the $this variable is a reference to the current object, which is itself, SimpleClass.  The this reference is commonly found in other object-oriented programming languages.

<?php
class SimpleClass
{
    public $greeting = 'Hi There';
 
    public function displayGreeting() {
        echo $this->greeting;
    }
}
 
//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayGreeting();
 
?>

The $this->greeting is to access its own variable $greeting.

If you access the $greeting variable without the $this reference, the browser will throw an undefined variable greeting error message.

You can try the below code to see the error message.

<?php
class SimpleClass
{
    public $greeting = 'Hi There';
 
    public function displayGreeting() {
        echo $greeting;
    }
}
 
//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayGreeting();
 
?>

this_error