Jump to content

Extending a class - why can't I access a parent method in a child


Recommended Posts

I have these two classes:

<?php
class A {
private $id;
public function __construct() {
	$this->id = '11111';
}
public function getProperty($prop) {
	return $this->$prop;
}
}?>

 

<?php
class B extends A {
private $message = "Nope, can't see it";
public function __construct() {
	parent::__construct();
	$this->message = "Yep, I can see it";
}
}?>

 

I tried this, but it just outputs the id (the first call)

$class = new B;
echo $class->getProperty('id');
echo $class->getProperty('message');

 

Why is that? I thought I could use a parent method on a child property..

 

 

*EDIT* Using a public or protected visibility on the message property gets me the output I expect...how come?

Little example that should help ya out.

 

<?php

Class A
{
    public function __construct()
    {
        echo 'Class A<br />';
    }
    
    public function say($string)
    {
        echo $string;
    }
}

Class B extends A
{
    public function __construct()
    {
        parent::__construct();
        echo 'Class B';
    }
}

$b = new B();
$b -> say('<br />hi');

?>

 

Outputs

 

Class A
Class B
hi

You need to turn on error reporting. If you did you would see the following error message:

Fatal error: Cannot access private property B::$message in C:\xampp\htdocs\test\test.php on line 9

 

Make the property public.

@kira:

Thanks for the reply.

 

I'm not sure that's exactly what I'm trying to do. I get that you can call a function that was defined in class A in class B by extending class A, but take a look at my example again..I defined a function inside A that returns a value. If I use it to return a property defined in class A it works, but not if I call it to return a property from class B (unless the property is public or protected). I thought that was the point of extending.. maybe I'm missing something.

 

Again, thanks for the reply. And sorry if I misunderstood your example

 

 

@mjdamato:

I did find that out, as I noted in the edit above. I'm wondering why. Any ideas? Thanks

(and I do have error reporting to E_ALL..never kicked out an error)

Private hides whatever it's attached to from everything except the class in which the private member was defined.

 

Protected hides whatever it's attached to from everything except the class in which the protected member was defined and any child classes.

 

In other words, it's working properly.

In other words, it's working properly.

 

I knew that :) just don't know why it is. I get the difference between visibilities. I just thought by extending Class A, I would be able to use a method from class A on Class B.

 

But I think I get what your saying. Even though I extended A, class B still considers A to be an outside class and therefore wouldn't allow access to B's private property?

 

I thought by extending it I could use it; that's where my confusion lies.

But I think I get what your saying. Even though I extended A, class B still considers A to be an outside class and therefore wouldn't allow access to B's private property?

 

Bingo, although you transposed A and B (A is the parent, denying B access to its private member).  Private means private, a.k.a., "Hands off my cookies (even you, Junior)!"  Protected means family, but no outsiders.

 

It's a useful distinction to have because, even if you want a class to be extended, there are rare occasions when you don't want certain data members to be accessible down the hierarchy chain.

 

Bingo, although you transposed A and B (A is the parent, denying B access to its private member).  Private means private, a.k.a., "Hands off my cookies (even you, Junior)!"  Protected means family, but no outsiders.

 

It's a useful distinction to have because, even if you want a class to be extended, there are rare occasions when you don't want certain data members to be accessible down the hierarchy chain.

 

I understand that. Thanks guys!

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.