1.What are PHP Traits?
ANS :
A Trait is simply a group of methods that you want
include within another class. A Trait, like an abstract class, cannot be instantiated on it’s own. More Ref: http://culttt.com/2014/06/25/php-traits/
1.A) How to call same method from different traits using php ? trait FileLogger{ public function log($msg){ echo 'File Logger ' . date('Y-m-d h:i:s') . ':' . $msg . '<br/>'; }} trait DatabaseLogger{ public function log($msg){ echo 'Database Logger ' . date('Y-m-d h:i:s') . ':' . $msg . '<br/>'; }} class Logger{ use FileLogger, DatabaseLogger{ FileLogger::log insteadof DatabaseLogger; }} $logger = new Logger();$logger->log('this is a test message #1');$logger->log('this is a test message #2');
2.How is it possible
to remove escape characters from a string?
ANS: The stripslashes function enables us to remove the
escape characters before apostrophes in a string.
3.Is it possible to
remove the HTML tags from data?
ANS: The strip_tags() function enables us to clean a string
from the HTML tags.
4.What does accessing
a class via :: means?
ANS: ‘::’ is used to access static methods
that do not require object initialization.
trait FileLogger{
public function log($msg){
echo 'File Logger ' . date('Y-m-d h:i:s') . ':' . $msg . '<br/>';
}
}
trait DatabaseLogger{
public function log($msg){
echo 'Database Logger ' . date('Y-m-d h:i:s') . ':' . $msg . '<br/>';
}
}
class Logger{
use FileLogger, DatabaseLogger{
FileLogger::log insteadof DatabaseLogger;
}
}
$logger = new Logger();
$logger->log('this is a test message #1');
$logger->log('this is a test message #2');
No comments:
Post a Comment