Traits vs. Interfaces in PHP with example
1.What is Traits in php?
- Traits are simply The way to share method class in your appication while keeping an equl eye on DRY code and making sure its re-usebility in the application. Used to declare methods that can be used in multiple classes. The methods can have any access modifier(public, private and protected).Traits is a mechanism for code reuse in single inheritance languages such as PHP.
- Traits are intended to reduce some limitation of single inheritiance by enabling a developer to reus?e sets of methods free in several indepedent class living different class hierarchies.
- Traits are importent part of PHP application As PHP does not support multiple inheritiances.
* declered with the keyword:
<?php
trait TraitName{
// some code
}
?>
So, assume we are having a USER and ADMIN classes , Which are required to use some methods with the same funtions.
<?php
trait HelloBajarangisoft {
public function Bajarangisoft() {
echo 'Loreum Impsum!';
}
}
class Bajarangisoftforb_soft {
use HelloBajarangisoft;
public function Bajarangisoft() {
echo 'The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using \'Content here, content here\', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for \'lorem ipsum\' will uncover many web sites still in their infancy.!';
}
}
$obj = new Bajarangisoftforb_soft();
$obj->Bajarangisoft();
?>
- Interface enables us to make programs, indicat the public methods that a class must execute, without including the complexities and procedure of how the specific methods are implemented. This implies that an interface can define method names and arguments, but not the contents of the methods.
Interface are defind in the same way as class, only the interface keyword replaces the class phrase in the declaration and without any of the methods having their contents defind.
declered with the keyword:
<?php
interface MyInterfaceName {
public function methodA();
public function methodB();
}
?>
<?php
// PHP program to demonstrate working
// of interface.
interface MyInterface{
public function message1();
public function message2();
}
class MyClass implements MyInterface{
public function message1(){
echo "Message(1) Called
Contrary to popular belief, Lorem Ipsum is not simply random text. <br />" . "\n";
}
public function message2(){
echo "Message(2) Called
It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.". "\n";
}
}
$object = new MyClass;
$object->message1();
$object->message2();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Traits vs Interfaces</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="text-center">
<h1>Traits vs. Interfaces in PHP with example</h1>
</div>
<br>
<h3>Using Traits Example:</h3>
<div class="well">
<?php
trait HelloBajarangisoft {
public function Bajarangisoft() {
echo 'Loreum Impsum!';
}
}
class Bajarangisoftforb_soft {
use HelloBajarangisoft;
public function Bajarangisoft() {
echo 'The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using \'Content here, content here\', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for \'lorem ipsum\' will uncover many web sites still in their infancy.!';
}
}
$obj = new Bajarangisoftforb_soft();
$obj->Bajarangisoft();
?>
</div>
<br>
<h3>Using Interface Example:</h3>
<div class="well">
<?php
// PHP program to demonstrate working
// of interface.
interface MyInterface{
public function message1();
public function message2();
}
class MyClass implements MyInterface{
public function message1(){
echo "Message(1) Called
Contrary to popular belief, Lorem Ipsum is not simply random text. <br />" . "\n";
}
public function message2(){
echo "Message(2) Called
It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.". "\n";
}
}
$object = new MyClass;
$object->message1();
$object->message2();
?>
</div>
</div>
</body>
</html>