PHP Tutorial Tutorials - PHP Property and method

PHP Property and method

What is Property?

All the houses share the same list of characteristics such as siding color.
Each house can have its own distinctive siding color, window, kitchen equipment and so on. These characteristics are the properties(attributes) of house object.

Color and window are two properties of the house.
First house has red color and 1 window.
Second house has blue color and 2 windows.

In PHP, variables within a class are called properties.
Properties can be added, removed and changed. Some properties can also be read-only.

the property can have a default value.

<?php
class House
{
    // property declaration
    public $color;
    // property with default value 
    public $numWindow = 1;
}
?>

You can use the symbol -> called object operator is used to access properties and methods of an object.

<?php
$objHouse = new House();
$objHouse -> $color = "red";
$objHouse -> $numWindow = 1;
?>

What is Method?

Method is action that is performed on Object.
For example,changeColor() is a method which performed on the house object.

In PHP,functions within a class are called methods.

<?php
class House
{
    // method declaration
    public function changeColor($newColor) {
        $this->color = $newColor;
    }
}
?> 

The keyword this refer to the calling object itself. It only can be used within the class.

Changing a property value using methods.

<?php
$objHouse->changeColor("blue");
?> 

Static Properties and Methods

Properties and methods can also be declared as static.
they can be accessed using the Scope Resolution Operator (::)
without instantiation of the class.

<?php
class House
{
    public static $owner = "Lautturi";
}
echo House::$owner;
?>

Magic Methods __get() __set()

If the class House has other properties such window ,room.
we have to create lots of setter and getter functions to get properties.

PHP provide some methods to optimize this process:
Magic Methods:__get() __set()
They MUST be declared as public.

__set() is run when writing data to inaccessible (protected or private) or non-existing properties.

__get() is utilized for reading data from inaccessible (protected or private) or non-existing properties.

<?php
class House {
    private $color = "red";
}
$house = new House();
echo $house -> color; // Fatal error: Uncaught Error: Cannot access private property House::$color
?>

Directly access the property is not allowed.
Let's add the magic method __get()

<?php
class House {
    private $color = "red";
    public function __get($name)
    {
        echo "Getting '$name'\n";
        if($name == "color"){
            return $this->color;
        }
        else{
            return "";
        }
    }
}
$house = new House();
echo $house -> color;
?>

Conclusion

So, four basic concepts are:

  • Class Create the house blueprint
  • Object Create the house from the blueprint
  • Property The color of house
  • Method Change the color
Date:2019-10-09 00:25:15 From:www.Lautturi.com author:Lautturi