Back to Top

PHP Scalar and Return type Declarations

Updated 6 August 2020

Hey, if you are looking into PHP scalar or return type declarations then you must read this post. These are the features that were introduced in PHP 7

Now let’s look closely with examples.

Scalar Type Declarations

This type of declaration has two options coercive (default mode) and strict

Coercive mode: it’s a default mode

<?php
class Customer {
  private $balance = 0;

  public function setWalletBalance(int ...$values) {
    $this->balance = array_sum($values);
  }

  public function getWalletBalance() {
    return $this->balance;
  }
}
$customer = new Customer();
$registration_reward = "1000";
$newsletter_reward = 100;

$customer->setWalletBalance($registration_reward, $newsletter_reward);

echo "My total balance is " . $customer->getWalletBalance();

In the above example, it’s just a simple customer class and method setWalletBalance() uses scalar type declaration which will accept any number of values then make a sum and then set to property balance.

Searching for an experienced
Opencart Company ?
Find out More

If you just look into variables $registration_reward and $newsletter_reward have different data types so in coercive / default mode the program will output “My total balance is 1100” without any error.

strict mode

<?php
declare(strict_types=1);
class Customer {
  private $balance = 0;

  public function setWalletBalance(int ...$values) {
    $this->balance = array_sum($values);
  }

  public function getWalletBalance() {
    return $this->balance;
  }
}
$customer = new Customer();
$registration_reward = "1000";
$newsletter_reward = 100;

$customer->setWalletBalance($registration_reward, $newsletter_reward);

echo "My total balance is " . $customer->getWalletBalance();

In the above example, I just enabled strict mode on the very first line of my program so now you will get a fatal

Return Type Declarations

It specifies the type of value which will be returned from a function

Default behavior:

<?php
class Customer {
  private $address = '';

  public function setAddress(string ...$values) {
    $this->address = implode("\n", $values);
  }

  public function hasAddress() : bool {
    return $this->address;
  }

  public function getAddress() : string {
    return $this->address;
  }
}
$customer = new Customer();
$name = "John Doe";
$address = "Webkul arv park";
$city = "Noida";
$zone = "Uttar Pradesh";
$country = "India";
$customer->setAddress($name, $address, $city, $zone, $country);

if($customer->hasAddress()) {
  echo "My address is " . "<pre>" . $customer->getAddress() . "</pre>";
} else {
  echo "No address found";
}

In the above example, I am just adding multiple values for an address using the scalar declaration as we looked into the previous example.

For method hasAddress(), it will return boolean value because the return type is bool but without any error then program output:

output

Strict mode:

<?php
declare(strict_types=1);
class Customer {
  private $address = '';

  public function setAddress(string ...$values) {
    $this->address = implode("\n", $values);
  }

  public function hasAddress() : bool {
    return $this->address;
  }

  public function getAddress() : string {
    return $this->address;
  }
}
$customer = new Customer();
$name = "John Doe";
$address = "Webkul arv park";
$city = "Noida";
$zone = "Uttar Pradesh";
$country = "India";
$customer->setAddress($name, $address, $city, $zone, $country);

if($customer->hasAddress()) {
  echo "My address is " . "<pre>" . $customer->getAddress() . "</pre>";
} else {
  echo "No address found";
}

If you enable the strict mode as I did in the above example at the very first line of my program then it will give a fatal because hasAddress() method is set to return only boolean value and address property has the string value

That’s it for this post, I hope it was helpful for you. visit more blogs here and check other PHP 7 features on the official site here.

If you have any query then ask in the comment section. Thanks

PHP
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home