This may sound a bit newby but I was wondering if you can use methods and properties from a child object

class Father{
	function Father(){
		// echo the child's $message
	}	
}

class Son extends Father{
	$public $message = 'We\'re in the same family!';
}

If you know what I mean, is there any way to do it?

Recommended Answers

All 3 Replies

Well first off there is an error in your code:

$public $message = "..."; //Not correct what's the first $ for
public $message = "..."; //Corrected!

And yes there is a way to do this. Since the son extends the father I would say simply use echo $this->message . If that doesn't work you could always make message a constant:

const message = "..."; //Notice that the constant name has no $ before it

With that code you could write message from the father with echo Son::message .

Thanks for the reply.

The error $public was created when I was rushing in notepad (no syntax highlighting) to make this example. Thanks however for correcting me.

Anyway, thanks for that response. I think that you might have mistook my question. I wanted to know how to use a child's (in this case Son's) methods or variables. None the less, I'll try out Son::message; or Son::$message; Thanks for the response. You are the first to respond to any of my threads.

In the way you're creating the variable, no. You can't do that in any language that does object orientation (or at least you shouldn't.) It wouldn't make any sense for Father to inherit the traits of Son. If you want to have a class that uses the traits of Son have it extend the Son class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.