Answers for "trait php"

PHP
1

trait in php example

<?php
/**
 * Inheritance in unnecessary function is also called we we don't require them, so we can prevent them using traits
 * Trait is only available when we use them otherwise not!
 */
trait A
{
    function fun1()
    {
        echo "class A called";
    }
}
class B 
{
    use A;
    function fun2()
    {
        echo "class B called";
    }
}
class C extends B
{
    function fun3()
    {
        echo "class C called";
    }
}
class D extends C
{
    use A;
    function fun4()
    {
        echo "class D called";
    }
}
$obj = new C();
$obj->fun1();
?>
Posted by: Guest on October-05-2021
1

trait php

<?php
trait Hello {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait World {
    public function sayWorld() {
        echo 'World!';
    }
}

trait HelloWorld {
    use Hello, World;
}

class MyHelloWorld {
    use HelloWorld;
}

$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
?>

  // L'exemple ci-dessus va afficher : Hello World !
Posted by: Guest on November-25-2020
1

php trait example

<?php
class Base {
    public function sayHello() {
        echo 'Hello ';
    }
}

trait SayWorld {
    public function sayHello() {
        parent::sayHello();
        echo 'World!';
    }
}

class MyHelloWorld extends Base {
    use SayWorld;
}

$o = new MyHelloWorld();
$o->sayHello();
?>
Posted by: Guest on March-19-2020

Browse Popular Code Answers by Language