-2

I am preparing for the ZEND Certified Engineer-Exam. Using the TestPassport-Engine "Virtual Exam", I came across this question:

Consider the following code. Which keyword should be used in line marked in bold to make this code work as intended?

abstract class Base {
    protected function __construct() {}

    public function create(){   
        // this line
        return new self();
    }

    abstract function action();
}

class Item extends Base {
    public function action () { echo __CLASS__; }
}

$item = Item::create();
$item->action();

And the correct answer is static. So, how should that look like in the end?

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • 3
    With the answer given, a little investigation of the keyword `static` should tell you where to put it.. It is really easier to spend two minutes on php.net than to type this whole question, especially if you are trying to learn PHP. I mean, the static method call is already there in the code. – GolezTrol Jul 16 '13 at 12:08
  • 7
    The fact that you are having to ask this question on StackOverflow just reinforces my personal belief that certifications like these are ultimately useless. "Certified Engineer" ... – crush Jul 16 '13 at 12:11
  • 2
    @crush - you hit the nail on the head, have my upvote! I couldn't agree more. – N.B. Jul 16 '13 at 12:12
  • @crush but you get shortlisted easily...:) – Arun Killu Jul 16 '13 at 12:23
  • 1
    @crush You can also say he is not ready for the exam. If someone ask a question like this does not mean the certification is useless. – Perry Jul 16 '13 at 12:23
  • Well, what can I say? Still learning - thanks for your encouragement. – MBaas Jul 16 '13 at 12:31
  • @Perry 1. Memorize answer to all exam questions. 2. ??? 3. Profit – crush Jul 16 '13 at 12:35
  • 1
    It's not a big secret that zend "certification" exists so that anyone can get a certificate for not-so-much money. It's also nice pocket money for Zend as well. No offense meant, but there are many Zend Certified people on SO who are spouting total nonsense all the time. It's cool for learning though. Good luck Baas and I hope you don't end up being one of the "bad" certified ones. – N.B. Jul 16 '13 at 13:06

1 Answers1

2

Simply change

public function create() {
    return new self();
}

to

public static function create() {
    return new static();
}

See here.

n3rd
  • 5,989
  • 4
  • 39
  • 56