PHP Posts

DB::DBAL DB::DBAL is a lightweight Database Abstraction Layer that is designed to allow for multiple database types and connections simultaneously. DB::DBAL provides ease of use of use through query chaining,...

Read more

Form Builder To be perfectly honest, this is a class I built awhile ago (back in November of 2010), so going from memory here. But basically, Form Builder allows you to setup a databased/ emailed form quickly and...

Read more

Weekly PHP Test What is the output of the following code? [php]echo (string) 042;[/php] ... ... ... ... ... ... ... ... ... ANSWER: [crayon]34[/crayon] Because the number starts...

Read more

Weekly PHP Test What does the following code echo out: [php] if('ORANGE' > 'apple') { echo 'orange'; } else { echo 'apple'; } [/php] ... ... ... ... ... ... ANSWER: [crayon]apple[/crayon] The...

Read more

PHP 5.4 – First Glances Updated February 10, 2012 PHP 5.4 is just around the corner, and with it several new features and enhancements.  Here are just a few of the features I've had the chance to play with that may be useful. Binary...

Read more

  • Prev
  • Next

Weekly PHP Test

Category : Weekly Test

True or False: In PHP 5.4 Traits can have properties?


..

..

..

..

..

..

..

..

..

..

ANSWER:

True!

In PHP 5.4 Traits may contain properties as long as properties do not exist in the calling class. But beware, if the property already exists PHP will throw an error (fatal if the property isn’t identical to the trait property, and E_STRICT if it is).

PHP
1
2
3
4
5
6
7
8
9
10
trait demo {
public $demo = 'this is a demo';
public $test = 'this is a test';
public function cool() {
/* this is the method */
}
/* ... */
}

Valid

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
trait testTrait {
public $demo = 'test';
}
class testClass {
use testTrait;
function test() {
echo $this->demo;
}
/* ... */
}

E_STRICT Error

PHP
1
2
3
4
5
6
7
8
9
10
11
trait testTrait {
public $demo = 'test';
}
class testClass {
use testTrait;
public $demo = 'test';
/* ... */
}

Fatal Error:

PHP
1
2
3
4
5
6
7
8
9
10
11
trait testTrait {
public $demo = 10;
}
class testClass {
use testTrait;
public $demo = 'test';
/* ... */
}

Fatal Error:

PHP
1
2
3
4
5
6
7
8
9
10
11
trait testTrait {
public $demo = 'test';
}
class testClass {
use testTrait;
protected $demo = 'test';
/* ... */
}

.

PHP Manual:
http://php.net/manual/en/language.oop5.traits.php

Facebook Twitter Linkedin Digg Reddit Stumbleupon Tumblr Posterous Email Snailmail
Share

Post a comment