18
loading...
This website collects cookies to deliver better user experience
firstName
and lastName
properties passed to the Employee
class through the constructor.<?php
class Employee
{
protected $firstName, $lastName;
public function __construct(string $firstName, string $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
}
$employee = new Employee('Ibrahim', 'Alausa');
firstName
and lastName
without any initial value. Then, in the body of the constructor, we set the firstName
and lastName
properties to the value passed to the constructor. In this example, that is **Ibrahim **as the value of the firstName
property and **Alausa **as the value of the lastName
property.Employee
class to use constructor property promotion. Let's see what our code will look like in this case<?php
class Employee
{
public function __construct(protected string $firstName, protected string $lastName)
{
}
}
$employee = new Employee('Ibrahim', 'Alausa');
$this
keyword since they are still within the function scope where they were declared. Let's see what our code will look like in this case<?php
class Employee
{
public function __construct(protected string $firstName, protected string $lastName)
{
var_dump($firstName); Ibrahim ✔️
var_dump($this->firstName); Ibrahim ✔️
}
}
$employee = new Employee('Ibrahim', 'Alausa');
$this
keyword to access all promoted properties so that PHP understands that you are referring to the firstName
or lastName
property of the class and not an undefined variable or a variable that may have been declared using the same name.<?php
class Employee
{
public function __construct(protected string $firstName, protected string $lastName)
{
}
public function getUndefinedFirstName()
{
var_dump($firstName); // Output: Undefined variable '$firstName' ❌
}
public function getWrongFirstName()
{
$firstName = 'John';
var_dump($firstName); // Output:John ❌
}
public function getFirstName()
{
var_dump($this->firstName); // Output:Ibrahim ✔️
}
}
$employee = new Employee('Ibrahim', 'Alausa');
Fname_
to every value passed to our Employee class as the first name and we want to do that cleanly. We don’t need to promote the firstName
property. Let's see what our code will look like in this case.<?php
class Employee
{
protected $firstName;
public function __construct(string $firstName, protected string $lastName)
{
$this->firstName = "Fname_$firstName";
}
public function getFirstName()
{
var_dump($this->firstName); // Output:Fname_Ibrahim ✔️
}
}
$employee = new Employee('Ibrahim', 'Alausa');
<?php
class Employee
{
protected $firstName;
public function __construct(protected string $firstName, protected string $lastName)
//Cannot redeclare Employee::$firstName ❌
{
$this->firstName = $firstName;
}
}
$employee = new Employee('Ibrahim', 'Alausa');
<?php
class Employee
{
public function __construct(protected string $firstName, protected string $lastName='N/A')
{
}
public function getLastName()
{
var_dump($this->lastName); //Output: N/A ✔️
}
}
$employee = new Employee('Ibrahim');
$this
keyword. However, class properties must be accessed with the this
keyword outside the constructor function.