Surfe.be - passive income

What are traits in PHP ?

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Traits have been available in PHP since 5.4. A trait is a PHP file that uses the trait keyword instead of class.One of the benefits of traits over classes and inheritance is the ability to use multiple traits inside one class. This is sometimes compared to multiple inheritance which is a feature PHP does not support.

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.

Example of trait

trait PHP {
	public function php_trait_method()
	{
		return "I love PHP";
	}
}

trait JavaScript {
   public function javascript_trait_method()
	{
		return "I love JavaScript";
	}
}

class example_one {
	use PHP;         // Use PHP trait in example_one class
}

class example_two {
	use JavaScript ;         // Use PHP trait in example_two class
}

$ex_one = new example_one;  // Creating example_one class object 
$ex_two = new example_two;  // Creating example_two class object

echo $ex_one->php_trait_method();
echo $ex_two->javascript_trait_method();

Here PHP and JavaScript are our two trait and example_one and example_two our two class . So we can say that , we can use trait in multiple class and access their data from multiple class . But you have to keep in mind that we can’t create an object of a trait .

Let’s see another example

trait PHP {
	public function php_trait_method()
	{
		return "I love PHP";
	}
}

trait JavaScript {
   public function javascript_trait_method()
	{
		return "I love JavaScript";
	}
}

class example_one {
	use PHP , JavaScript;         // Use PHP trait in example_one class
}

$ex_one = new example_one;  // Creating example_one class object 

echo $ex_one->php_trait_method();
echo $ex_one->javascript_trait_method();

Here PHP and JavaScript are our two trait and example_one is our only class. But have a look in example_one class . We can declare multiple traits in a single class and can access their multiple traits data from a single class . Hope you got it .

Merging Trait

trait PHP {
	public function php_trait_method()
	{
		return "I love PHP";
	}
}

trait JavaScript {
   public function javascript_trait_method()
	{
		return "I love JavaScript";
	}
}

trait merging_PHP_JavaScript_together{
	use PHP , JavaScript ;
}

class example_one {
	use merging_PHP_JavaScript_together ;   // Use merging_PHP_JavaScript_together trait here
}

$ex_one = new example_one;  // Creating example_one class object 

echo $ex_one->php_trait_method();
echo $ex_one->javascript_trait_method();

Have a look in merging_PHP_JavaScript_together trait . We can merge multiple trait in a single trait and can access data like before .

Precedence Order Example

trait PHP {
	public function php_trait_method()
	{
		return "I am in PHP trait";
	}
}

class example_one {

	use PHP ;   // Use PHP trait here

	public function php_trait_method()
	{
		return "I am inside example_one class";
	}
}

$ex_one = new example_one;  // Creating example_one class object 

echo $ex_one->php_trait_method();

The above example will output:

I am inside example_one class

Why ? Because class example_one would give priority of his own class method . Hope you will understand all those above discussion .

How to access class method from a trait ?

class Universe {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait Hello_World{
    public function sayHello() {
        parent::sayHello();  // Accessing a class method using parent keyword with :: sign from a trait
        echo 'World!';
    }
}

class World extends Universe {
    use Hello_World;
}

$o = new World();
$o->sayHello();

The above example will output:

Trait
Hello World!

Conflict Resolution

If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.

To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to choose exactly one of the conflicting methods.Since this only allows one to exclude methods, the as operator can be used to add an alias to one of the methods. Note the as operator does not rename the method and it does not affect any other method either. Learn more about it conflict resolution .

Accessing static method

trait StaticExample {
    public static function doSomething() {
        return 'Doing something';
    }
}

class Example {
    use StaticExample;
}

Example::doSomething();

Hope you will understand all those above discussion . If you think you learn from some here please share this tutorial with your friends and get subscribe to get new post notification .

 

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST