0
0
PHPprogramming~20 mins

Trait declaration and usage in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trait Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of trait method usage
What is the output of this PHP code using a trait?
PHP
<?php
trait Hello {
    public function sayHello() {
        return "Hello from trait!";
    }
}

class Greeter {
    use Hello;
}

$g = new Greeter();
echo $g->sayHello();
?>
AFatal error: Trait method sayHello not found
BParse error: syntax error, unexpected 'use'
CHello from class Greeter!
DHello from trait!
Attempts:
2 left
💡 Hint
Traits allow classes to reuse methods defined inside them.
Predict Output
intermediate
2:00remaining
Trait method conflict resolution
What will be the output of this PHP code with two traits having the same method name?
PHP
<?php
trait A {
    public function message() {
        return "Message from A";
    }
}

trait B {
    public function message() {
        return "Message from B";
    }
}

class Test {
    use A, B {
        B::message insteadof A;
    }
}

$obj = new Test();
echo $obj->message();
?>
AMessage from B
BMessage from A
CFatal error: Trait method message has not been applied
DParse error: syntax error, unexpected 'insteadof'
Attempts:
2 left
💡 Hint
The insteadof keyword resolves method conflicts by choosing one trait's method.
🔧 Debug
advanced
2:00remaining
Identify the error in trait usage
What error will this PHP code produce?
PHP
<?php
trait Logger {
    public function log($msg) {
        echo $msg;
    }
}

class Service {
    use Logger
    public function run() {
        $this->log("Running service\n");
    }
}

$s = new Service();
$s->run();
?>
AOutput: Running service
BFatal error: Trait Logger not found
CParse error: syntax error, unexpected 'public' (missing semicolon after use statement)
DWarning: Undefined method log
Attempts:
2 left
💡 Hint
Check punctuation after the use statement inside the class.
🧠 Conceptual
advanced
2:00remaining
Trait properties and visibility
Consider this PHP code using a trait with a property. What will be the output?
PHP
<?php
trait Counter {
    private int $count = 0;
    public function increment() {
        $this->count++;
    }
    public function getCount() {
        return $this->count;
    }
}

class Clicker {
    use Counter;
}

$c = new Clicker();
$c->increment();
$c->increment();
echo $c->getCount();
?>
AFatal error: Cannot access private property
B2
C0
DParse error: syntax error
Attempts:
2 left
💡 Hint
Traits can have properties with visibility, and the class uses them as if declared inside it.
🚀 Application
expert
3:00remaining
Using multiple traits with method aliasing
Given this PHP code, what is the output?
PHP
<?php
trait T1 {
    public function greet() {
        return "Hello from T1";
    }
}

trait T2 {
    public function greet() {
        return "Hello from T2";
    }
}

class MyClass {
    use T1, T2 {
        T1::greet as greetT1;
        T2::greet insteadof T1;
    }
    public function greet() {
        return $this->greetT1() . " and " . T2::greet();
    }
}

$obj = new MyClass();
echo $obj->greet();
?>
AFatal error: Cannot call trait method statically
BHello from T2 and Hello from T1
CParse error: syntax error
DHello from T1 and Hello from T2
Attempts:
2 left
💡 Hint
Check how trait methods are called inside the class method.