Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

0 votes
1 answer
743 views

why can't I access a protected method from a private method if the protected ones are inherited by the subclasses?

I do not understand this particular case or what Alexander says. class instance is not the same as class? As already Alexander Larikov said that you can't access protected methods from class instance ...
user avatar
2 votes
2 answers
719 views

PHP: understanding $this - calling base class method instead of child method

Reading the PHP.net documentation I stumbled upon a problem that twisted the way I understand $this: class C { public function speak_child() { //echo get_class($this);//D echo $...
Jack Scandall's user avatar
0 votes
1 answer
536 views

Parent's protected properties inaccessible to child object

I have a parent class I'll be calling 'ParentClass' and a child class (that extends from it) which I'll be calling 'ChildClass'. ParentClass has protected properties $prop1 and $prop2 which I want ...
Douglas Silva's user avatar
0 votes
0 answers
38 views

How to make available only those public methods which are declared in interface?

In Java, for example, this comes out of the box due to static nature of the language. But in PHP this could be also useful especially to keep a code secure and clean from an architectural point of ...
Mikhail Karakulov's user avatar
3 votes
1 answer
3k views

child class accessing parent's private method?

A user posted a comment on PHP Visibility manual page. It's the second most voted comment. He used this code example: <?php abstract class base { public function inherited() { $this-&...
Pontiac_CZ's user avatar
2 votes
0 answers
131 views

Why isn't php happy with a public method signature being incompatible with the parent private method signature?

The following piece of code <?php class A { private function test($str = null) { echo $str; } } class B extends A { public function test() { echo 'b'; } } $b = ...
greg0ire's user avatar
  • 23.1k
1 vote
1 answer
192 views

Can't access parent class properties

When i use parent class properties it returns NULL , I have no idea why this happens, Example code : class Foo { public $example_property; public function __construct(){ $this->...
Amin Abdolrezapoor's user avatar
6 votes
3 answers
5k views

How to prevent usage of public parent methods in child instance in PHP?

So let's say I have classes called parent and child, which will be then used from PHP file called caller.php class Child extends Parent { } class Parent { public function parentMethod(){ }...
Firze's user avatar
  • 4,019
2 votes
1 answer
268 views

php inheritance with protected array property is giving me a serious headache

So I decided to make some classes in PHP so I can dynamically construct a menu bar using <ul>, <li> and <a> tags. Reason why is because I want to reuse this across projects. First the Blob ...
Jif's user avatar
  • 843
0 votes
4 answers
616 views

OOP inheritance in PHP

I have some problems with OOP inheritance in PHP. I have 2 class, Animal and Penguin. Penguin extends Animal. My source code like this : class Animal{ public $type = "animal"; function dance(){ ...
moogod's user avatar
  • 9
2 votes
4 answers
13k views

Calling a child method from the parent class in PHP

Having the following class hierarchy: class TheParent{ public function parse(){ $this->validate(); } } class TheChild extends TheParent{ private function validate(){ ...
Songo's user avatar
  • 5,698
14 votes
4 answers
9k views

Why does PHP allow protected and private methods to be made public via an override in subclasses?

From some brief fiddling about, I find I get an error when overriding superclass methods in a subclass when I do the following: Override a protected superclass method with a private subclass method ...
Matt Gibson's user avatar
  • 14.9k
2 votes
1 answer
6k views

Calling an overriden static method from parent

I have two classes. A child and a parent. The parent is calling a static method from the child (that's an overriden static parent method in the child class) and I get a general server error. When I ...
Martijn's user avatar
  • 1,390
0 votes
1 answer
233 views

doctrine2 and codeigniter2, right approach for parent class to interact with child properties

I'm searching for this for quite some time now. I saw few similar questions but not sure if they apply with doctrine2 and this problem. I'm trying to get started with CodeIgniter2 and Doctrine2, and I'...
Dalibor's user avatar
  • 167
10 votes
3 answers
12k views

PHP Inherited parent method can't access child's private property

First of all: A quite similar problem has been posted and somehow solved already, but is still not answering my specific problem. More about this later. In words: I have a base class which provides ...
Jan's user avatar
  • 103

15 30 50 per page