Development

PHP OOP Setters and Getters

Setters and Getters in PHP are object methods used in OOP programming. The basis being you set a property to your value and then you can get it.

Using setters and getters has the advantage of being a clear and clean process, having validation and defined debugging along with greater flexibility.

Using OOP is subjective, from my limited use and obvious statements it is best used for large projects where the code will be shared and/or worked on by many.

Setters

The setters, set a value for a property:

<?php
class account //account class
{
    private $account_id;
    private $created;
    private $owner_id;
    private $balance;

    public function setAccount($account_id) //Setter
    {
        $this->accountId = $account_id;
    }

    public function setCreated($created) //Setter
    {
        $this->accountCreated = $created;
    }

    public function setOwner($owner_id) //Setter
    {
        $this->accountOwner = $owner_id;
    }

    public function setBalance($balance) //Setter
    {
        $this->accountBalance = $balance;
    }
    
}

Getters

The getters, get (return) what has been set:

public function getAccount(){ //Getter
    return $this->accountId;
}

public function getCreated(){ //Getter
    return $this->accountCreated;
}

public function getOwner(){ //Getter
    return $this->accountOwner;
}

public function getBalance(){ //Getter
    return $this->accountBalance;
}

These would be included in the class account.

Now to set and get, call and assign a new instance of the class account. Set the account as 12345 and then echo the set account.

$ac = new account();
$ac->setAccount(12345);
echo $ac->getAccount();//12345

To dig further:

$ac = new account();
$ac->setAccount(12345);
$ac->setOwner('Garry');
$ac->setBalance(455);
echo $ac->getAccount().' '.$ac->getOwner().' '.$ac->getBalance();//12345 Garry 455

Not seen above in the class is a very important meaning as to why using Setters and Getters is actually of benefit. The reason being validation.

Validation

Preventing errors or circumstances when a wrong input will break a whole class.

public function setAccount($account_id)
{
    if (!is_int($account_id) or trim($account_id) == '') {
        throw new Exception("You must provide an Owner");
    }
    $this->accountId = $account_id;
}

The above will check that when setting an account it is only an INT and it cannot be empty otherwise an error ill be thrown.

You can run the same checks for STRINGS or FLOATS.

public function setOwner($owner_id)
{
    if (strlen($owner_id) > 60) {
        throw new Exception("Owner cannot be more than 60 characters");
    }
    $this->accountOwner = $owner_id;
}

The above only allows the owner id to be less than 60 characters otherwise an exception gets thrown. This is the basis for setters and getters in PHP OOP.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago