How can I prevent the something method below to be created in the foo class ?

class fooBase{

  public function something(){

  }
}

class foo extends fooBase{

  public function __construct(){
    echo $this->something(); // <- should be the parent class method
  }

  public function something(){ 
    // this method should not be allowed to be created
  }
}

Accepted Answer

Use the final keyword (like in Java etc):

class fooBase{

  final public function something(){

  }
}

class foo extends fooBase{

  public function __construct(){
    echo $this->something(); // <- should be the parent class method
  }

  public function something(){ 
    // this method should not be allowed to be created
  }
}

See PHP Final keyword. Note that foo will still have a method something, but something will only come from fooBase and foo can't override it.

Written by MGwynne
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki