Jump to content

Scope/Visibility Question about Child/Parent class


Go to solution Solved by KevinM1,

Recommended Posts

So I have an instance of UserXyzPersister which extends DBPersisterAbstract which extends DBPersisterBase.

 

There is a function in the DBPersisterBase class that is protected:

protected function getArray($arg){
...
}

I'm trying to access it via:

$u = new UserXyzPersister();
$u->getArray($arg);

This is working. My question is, can only the children of DBPersisterBase access getArray() or can the parents as well?

  • Solution

So I have an instance of UserXyzPersister which extends DBPersisterAbstract which extends DBPersisterBase.

 

There is a function in the DBPersisterBase class that is protected:

protected function getArray($arg){
...
}
I'm trying to access it via:

$u = new UserXyzPersister();
$u->getArray($arg);
This is working. My question is, can only the children of DBPersisterBase access getArray() or can the parents as well?

 

If something is declared public or protected, then only the children of that class gain access to it. Inheritance is a one-way street.

 

EDIT: and by children, I mean any class down the inheritance chain from the original. Try it yourself:

 

class A
{
    protected function hello()
    {
        echo "Hello from class A!";
    }
}

class B extends class A 
{
    protected function test()
    {
        echo "Hello from class B!";
    }
}

class C extends class B {}

$a = new A();
$c = new C();

$c->hello();
$a->test();
Edited by KevinM1

It has been my experience that protected methods are available up and down the inheritance tree.

 

<?php

error_reporting(E_ALL);
ini_set('display.errors', 1);

class A {
	protected function hello () {
		print("Hello from A\n");
	}
	
	public function askMe() {
		print("A says:\n");
		$this->hello();
		$this->test();
	}
}

class B extends A {
	protected function test() {
		print("Hello from B\n");
	}
	
	public function askMe() {
		print("B says:\n");
		$this->hello();
		$this->test();
		print("Ask A ...");
		parent::askMe();
	}
}

class C extends B {
	
	public function askMe() {
		print("C says:\n");
		$this->hello();
		$this->test();
		print("Ask B ...");
		parent::askMe();
	}
}

print("Version: " . phpversion() . "\n\n");
$c = new C;
$c->askMe();
$c->test();
$c->hello();

/* 
Version: 5.3.4

C says:
Hello from A
Hello from B
Ask B ...B says:
Hello from A
Hello from B
Ask A ...A says:
Hello from A
Hello from B
Fatal error: Call to protected method B::test() from context '' in D:\wwwIntranet\wwwDEV\public\inhTest.php on line 46

If you comment out line 46, then you get this error:
Fatal error: Call to protected method A::hello() from context '' in D:\wwwIntranet\wwwDEV\public\inhTest.php on line 47
*/
From an OOP standpoint, it doesn't make sense for A to call test() since it is not defined until B; unless of course, A defines it as abstract so we know it will be there later.

 

You cannot call hello() or test() from outside of the class, because they are protected.

 

I think KevinM1 is answering questions in his sleep, again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.