Hi,
I am implementing an abstract class in PHP with some properties in the base class. How do I ensure that these will all this be carried down to children that extend the class?
Regards,
Cyril
5 replies to this topic
#1
Posted 28 January 2011 - 12:41 AM
|
|
|
#2
Posted 28 January 2011 - 04:24 AM
You just need to extend a class... that's all,
if you do something in construcor method of abstract class - you probably need to call it in childs constructor as or something like that -
if you do something in construcor method of abstract class - you probably need to call it in childs constructor as or something like that -
parent::__construct(args);Also if you want to be oversure - then you might do some variables checking)
#3
Posted 28 January 2011 - 09:19 PM
That is how inheritance works.
class A
{
protected var $a;
}
class B extends A
{
public void nonsensicle()
{
$this->a = 10;
echo $this->a;
}
}
#4
Posted 28 January 2011 - 09:28 PM
This also should work, and for syntax - there is no 'var' after protected...
thanks,I forgot about that)
thanks,I forgot about that)
- public - can be enherited;
- protected - can be enherited;
- private - can NOT be enherited
class A
{
protected $a = 10;
}
class B extends A
{
public void nonsensicle()
{
echo $this->a;
}
}
#5
Posted 01 February 2011 - 09:40 AM
Thanks, I was not aware of the fact that the abstract class could have some features declared in it and inherited.
#6
Posted 01 February 2011 - 11:40 AM
Abstract can have as much or as little as you want declared, you can even have a full declaration. if you just declare it abstract, the only thing is that you can't instantiate from it, it needs to be inherited first.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









