2

Is there a way to prevent overriding properties in PHP? And if yes - how?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
user997379
  • 25
  • 1
  • 5

4 Answers4

4

I think this is what you want:

class A{
    private $a;
    public function __construct(){
        $this->a = 'A';
    }
    public function __get($property){
        return $this->{$property};
    }
}

class B Extends A{
    public function getA(){
        return $this->a;
    }
    public function __get($property){
        return parent::__get($property);
    }
}

$b = new B;
echo $b->getA();
dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • 1
    That only works if you want a method instead of a property, and if you control the child class definition. – Jared Farrish Nov 23 '11 at 23:54
  • No :(. I wan't to call this property AS PROPERTY :D Thanks. – user997379 Nov 23 '11 at 23:59
  • @user997379 - You're going to have to show how you're needing it implemented; short answer, it doesn't sound possible for a [`final`-like](http://en.wikipedia.org/wiki/Final_%28Java%29) functionality in PHP without using a method. – Jared Farrish Nov 24 '11 at 00:01
  • Can yo please post some code with explanation where do you want to prevent property from being overridden and how you want to access them? – dev-null-dweller Nov 24 '11 at 00:07
  • @JaredFarrish comment gave me a hint, if you don't want parent classes to have their own property named just as in base class, it is not possible in the moment (for properties, methods can be `final`). – dev-null-dweller Nov 24 '11 at 00:13
  • @JaredFarrish: `const` is used for "final" properties, it just has the disadvantage of controlling the visibility as well. – chelmertz Nov 24 '11 at 23:40
1

The only way to do such a thing is playing with __set() in a class. But, __set is only used for non accessible properties.

For public properties, i don't think there is any way to prevent overriding.

You should check out the official documentation: http://fr2.php.net/manual/en/language.oop5.overloading.php

Lao
  • 191
  • 3
  • Thanks, but I know what means __set and can't help me, becouse my properties are declared. – user997379 Nov 23 '11 at 23:45
  • So i don't think what you want to do is possible... Try to find another way: store these protected variables in a private array, and access them using a specific __get – Lao Nov 23 '11 at 23:47
  • So whatever, I undeclared my properties and ... :) – user997379 Nov 24 '11 at 00:12
1
class TheBaseClass {

  private $nomod = 'foo';

  public function getNoMod() {
     return $this->nomod;
  }
}

class TheChildClass extends TheParentClass {
   public function funcUsesNomod(){
      return 'The value of nomod is '. $this->getNomod();
   }
}
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • 1
    To be fair to @dev-null-dweller, the OP seems to suggest a method won't work (it must be a property, for some reason). I have given you both upvotes, though. – Jared Farrish Nov 23 '11 at 23:57
0

"Yeah", if you make them constants. See the answers to How to implement a read-only member variable in PHP?, don't skip the comments.

Example:

class user {
  const ALWAYS_ONE = 1;
}

I guess, declaring properties as private also prevents overloading but they are also inaccessible to the descending classes.

Community
  • 1
  • 1
chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • Thanks, but I need to call this property in child classes (it contains object) with a "property interface" – user997379 Nov 23 '11 at 23:41
  • 1
    I think @Jared Farrish is right, use a method. (Hard to tell when you don't spell out the full scenario) – chelmertz Nov 23 '11 at 23:48