Jump to content

Recommended Posts

I just started my journey into OOP, just looking for a simple explanation as to why the script echo's nothing. (last line)

 

<?php
class Dog {
	public $name;
	public function bark() {
		echo "{$this->name} says Woof!";
	}
}

class Poodle extends Dog {
	public function bark() {
		echo "Yip";
	}
}

$furball = new Poodle;
$furball->name = "furball";
$furball->Dog::bark();
?>

Link to comment
https://forums.phpfreaks.com/topic/258544-scope-resolution-operator/
Share on other sites

$furball->Dog::bark();

That's a syntax error. You cannot use $furball to call Dog's bark() method. You can, however, put something in Poodle that will.

 

But what (I think) you're trying to do should look more like

class Dog {

public $name;

public function bark() {
	echo "{$this->name} says {$this->getBarkNoise()}!";
}

protected function getBarkNoise() {
	return "Woof";
}

}

class Poodle extends Dog {

protected function getBarkNoise() {
	return "Yip";
}

}

$furball = new Poodle();
$furball->name = "furball";
$furball->bark();

I'm not really trying to do anything per-say, just fiddling to understand. Thanks for taking the time though ^^.  I still don't understand why that would be a syntax error, since Poodle extends Dog, why can't I use :: to call bark() from the parent class? Sorry for the original question being so vague.

Because PHP doesn't let you do that. And it kinda violates object-oriented design.

 

Poodle has chosen to hide Dog's bark() method with its own implementation for whatever reason (that you may or may not know about). You are not allowed to "go around" that and call Dog::bark() instead.

$furball is an instance of Poodle, not Dog. You can't call parent fuctions (that I  know of) directly from an instance of a child function. If you wanted to add this functionality to the object, you can do this:

 

class Poodle extends Dog {
	public function bark() {
		echo "Yip";

	}

	public function callParent(){

		parent::bark();	
	}
}

 

And then do a $furball->callParent().

From outside a class, the scope resolution operator is mostly used to call static functions (functions inside a class can be called without instantiating an object of the class). So if you wanted to add a static function, you could do this:

 

class Poodle extends Dog {
	public function bark() {
		echo "Yip";

	}

	public function callParent(){

		parent::bark();	
	}

	Public static function callMe(){
		echo "Meow!";	
	}
}

Poodle::callMe();

 

Notice that I didn't need to instantiate an actual object to call the static function.

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.