WeylynCadwell
WeylynCadwell

Reputation: 317

Using traits in PHP

I can't seem to find a good thread explaining this, but are traits in PHP the same (more or less) as structs in C++? I understand the basic syntax for the traits for PHP as well as some more intermediate structs for C++, but are they used in the same way or does PHP have different standards for their use than C++?

Also, since there seems to be no public/private differences such as C++ (where a class is default private until 'public:' or 'protected:' is specified whereas a struct is public until 'private:' or 'protected:' is specified), or is the same general rules?

Upvotes: 1

Views: 771

Answers (2)

Aniket Singh
Aniket Singh

Reputation: 2634

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

trait Hello
{
   function sayHello() {
       echo "Hello";
   }
}

trait World
{
   function sayWorld() {
       echo "World";
   }
}

class MyWorld
{
   use Hello, World;
}

$world = new MyWorld();
echo $world->sayHello() . " " . $world->sayWorld();  //Hello World

Basically Traits are introduced to avoid some problems in case of inheritance, These are the common problems in class or interface like

Problem in class:

In a traditional you are not allowed to create abstract methods and class can not be inherited by another class if it is inheriting any other class.

Problem in Abstract class:

In Abstract class you can create abstract method as well body methods but whenever a class will inherit this abstract class, it will have to define the body of all abstract methods of the abstract class. Also this abstract class can not be inherited by another class if it is inheriting any other class same as a normal class.

Problem in Interface:

By using interface you can achieve multiple inheritance in class in PHP but in an interface you are not allowed to define any body of a method. You can only have abstract methods and whenever you are going to implement this in a class, you have to define body of all methods.

Upvotes: 1

Olavi Sau
Olavi Sau

Reputation: 1649

A trait is a partial class implementation (i.e., constants, properties, and methods) that can be mixed into one or more existing PHP classes. Traits work double duty: they say what a class can do (like an interface), and they provide a modular implementation (like a class).

"Modern PHP",978-1-491-90501-2, Page 17

It's not quite like structs, a train is a socalled addon or a plugin to a class, meanwhile a struct is more like a collection object. When you use a trait, it is basically injected into the class, something similar in c++ is multiple inheritance.

<?php
    trait MyTrait {
        protected $myField;

        public function setMyField($value){
             return $this->myField = $value;
        }
        public function getMyField(){
             return $this->myField;
        }
    }


?>

<?php
class MyClass
{
    use MyTrait;
}

$myClass = new MyClass();
$myClass->setMyField("Hello World");
echo $myClass->getMyField();//echo's Hello World

Upvotes: 0

Related Questions