Simple Trait in PHP PRO

Defining a trait is very similar to defining a class. Instead of using the keyword class we use the keyword trait. The purpose of traits is to group functionality in a fine grained and consistent way. It is not possible to instantiate a trait. The trait servers as an additional capability that provides us with additional capabilities when using inheritance in our code. The class we define can extend one class at the most and implement interfaces and/or uses traits as many as we want. The following video clip shows a simple trait definition.

<?php
trait Academic
{
    function think()
    {
        echo "i m thinking!";
    }
}

class Person
{
    private $id;
    private $name;
    function __construct($idValue,$nameValue)
    {
        $this->id = $idValue;
        $this->name = $nameValue;
    }
    function __toString()
    {
        return "id=".$this->id." name=".$this->name;
    }
}

class Student extends Person
{
    use Academic;
    private $avg;
    function __construct($idVal,$nameVal,$avgVal)
    {
        parent::__construct($idVal,$nameVal);
        $this->avg = $avgVal;
    }
    function __toString()
    {
        $str = parent::__toString();
        return "avg=".$this->avg.$str;
    }
}

class Lecturer extends Person
{

    private $degree;
    function __construct($idVal,$nameVal,$degreeVal)
    {
        parent::__construct($idVal,$nameVal);
        $this->degree = $degreeVal;
    }
    function __toString()
    {
        $str = parent::__toString();
        return "degree=".$this->degree.$str;
    }
    use Academic;
}

$student = new Student(123123,"mosh",98);
$lecturer = new Lecturer(42343,"dan","mba");

$student->think();
echo "<hr/>";
$lecturer->think();

?>

The following video clip shows the execution of this code sample and explains it.

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences