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 & 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: Constructors and destructors

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

As an object-orientted programming language, PHP 5 allows developers to declare constructor methods for classes.

Classes which have a constructor method will call this method on each newly-created object.

So in a way, constructor is used to automate the task of creating a new object.

The destructor method will be called when there are no references to a particular object, or during the shutdown sequence.

In the code below, the destructor will be called when the running of php script is completed.

<?php

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

    function __construct($pop, $sal, $alb )
    {
        echo "Construct function automatically called";
        echo "<br>";
        $this->person = $pop;
        $this->salary = $sal;
        $this->album = $alb;
    }

    function __destruct(){
        echo "Destruct function automatically called";
        echo "<br>";
    }

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

$star = new PopStar("Madonna", 10000, "Crazy For You");
echo "The pop star salary is:";
echo "<br>";
$star->getSalary();
echo "<br>";

?>

The output from running the above script is:

construct

PHP & OOP: Visibility

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

The visibility of a property or method can be defined by the keywords public, protected or private.

Property (variable) visibility

Class members declared public can be accessed everywhere.

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

Members declared as private may only be accessed by the class that defines the member.

Lets look at the example below.

<?php

class MyClass
{
    public $public_str = 'I am a public string';
    protected $protected_str = 'I am a protected string';
    private $private_str = 'I am a private string';

    function printString()
    {
        echo $this->public_str . "<br>";

        echo $this->protected_str . "<br>";
        echo $this->private_str . "<br>";
    }
}

$obj = new MyClass();
$obj->printString(); // Shows Public, Protected and Private, pringString() is a public function
echo "<br>";
echo $obj->public_str; // Works
// echo $obj->protected; Fatal Error as $protected is accessed outside the class and not by an inherited class
// echo $obj->private; Fatal Error as $private is accessed outside the class

?>

The 3 member variables $public_str, $protected_str, $private_str can be assessed using the function printString() as it is a member function

printString() is a public function and so you can access it to print out the 3 variables.

$obj->protected_str:  This will produce a fatal error as $protected is accessed outside the class and not by an inherited class

$obj->private_str:  This will generate a fatal error as $private is accessed outside the class.

PHP & OOP: Method visibility

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

Class methods may be defined as public, private, or protected.

Methods declared without visibility keyword are defined as public.

<?php

class MyClass
{
    // Declare a public constructor
    public function __construct()
    {
    }

    // Declare a public method
    public function MyPublic()
    {
        echo "I am a public function";
        echo "<br>";
    }

    // Declare a protected method
    protected function MyProtected()
    {
        echo "I am a protected function";
        echo "<br>";
    }

    // Declare a private method
    private function MyPrivate()
    {
        echo "I am a private function";
        echo "<br>";
    }

    // This is public
    function printString()
    {
        $this->MyPublic();
        $this->MyProtected();
        $this->MyPrivate();
    }
}

$myclass = new MyClass;
$myclass->MyPublic(); // Works
echo "--<br>";
// $myclass->MyProtected(); Fatal Error
// $myclass->MyPrivate(); Fatal Error
$myclass->printString(); //Public, Protected and Private work

?>

We look at these 2 statements:

$myclass->MyProtected();

-Fatal Error as MyProtected() is protected and can only be accessed by inherited class.

$myclass->MyPrivate();

Fatal Error as MyPrivate() is a private function and can only be accessed in its own class.