Reputation: 9
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(){
echo $this->type." dances!\n";
}
}
and,
class Penguin extends Animal {
private $type = "penguin";
}
And, my main code is like this;
include('animal.php');
include('private_penguin.php');
$tux = new Penguin();
$tux->type = "linux penguin";
$tux->dance();
This is all my code. I read OOP in some tutorial. But there is lik this code, there is a Fatal error: Access level to Penguin::$type must be public. But i have dont. Which one is correct!
in Animal class $type is public, but Penguin class is private;
Can you help me!
Upvotes: 0
Views: 619
Reputation:
Whenever you will extend or inherit any call in your child class then your parent method or properties will be overrided from child. So in your case your public $type = "animal";
from animal.php is getting overrided by your child private $type = "penguin";
So your $this->type
will always have character from penguin class, even in the animal class also(because you have extended animal class in your penguin class). This is very basic phenomena of object oriented programming.
Upvotes: 1
Reputation: 518
your private variable is inside Penguin
class . So you cant use it outside the scope of this class . Also when you are extending a class . you dont have to include the animal.php on you main page also . you can use it in private_penguin.php only to extend .
So the code for Penguin class should be like this :
include('animal.php');
class Penguin extends Animal {
private $type = "penguin";
function getPrivateData()
{
echo $this->type;
}
}
then you can call it in you main page .
include('private_penguin.php');
$tux = new Penguin();
$tux->type = "linux penguin";
$tux->getPrivateData();
this will do it with private variable .
Upvotes: 0
Reputation: 45124
I guess if would be if you can go through the Visibility in PHP. Simply Private variable not allowed to be accessed you are doing right now. What you can do is to
class Penguin extends Animal {
public $type = "penguin"; // else keep it as public without changing as in the Animal class
}
You can write a method as shown below to access the private $type
variable.
Extracted from docs for the ease of reference
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
Upvotes: 0
Reputation: 360572
private variables cannot be accessed from outside
the class. e.g.
class Penguin {
function setType() {
$this->type = 'penguin'; // works
}
}
$tux->type = 'penguin'; // does not work
that's the whole point of having private variables - to prevent "outside" code from messing around with the innards of an object.
Upvotes: 3