Is there a way to prevent overriding properties in PHP? And if yes - how?
Asked
Active
Viewed 8,098 times
2
-
2You mean like [public, protected or private](http://php.net/manual/en/language.oop5.visibility.php)? – Jared Farrish Nov 23 '11 at 23:32
-
I have a protected property in class Base called pbase (as example) and I want to prevent overriding the property in child classes. – user997379 Nov 23 '11 at 23:36
-
4Make it [`private`](http://codepad.org/9sxXFi2X)? – Jared Farrish Nov 23 '11 at 23:36
-
When I make it private, I can't call it from child classes, but I want to prevent only overriding. – user997379 Nov 23 '11 at 23:38
-
3I think you need a method to retrieve your value, not a plain property. – Jared Farrish Nov 23 '11 at 23:40
-
Please provide more context to your question. – Jared Farrish Nov 23 '11 at 23:52
4 Answers
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
-
1That 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
-
-
@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
-
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
-
1To 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.
-
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
-
1I 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