Jump to content

Inheritance of properties in base abstract class

- - - - -

  • Please log in to reply
5 replies to this topic

#1
rhossis

rhossis

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
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

#2
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
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 -
parent::__construct(args);
Also if you want to be oversure - then you might do some variables checking)

#3
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
That is how inheritance works.


class A 

{

  protected var $a;

}


class B extends A

{

  public void nonsensicle()

  {

    $this->a = 10;

    echo $this->a;

  }

}



#4
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
This also should work, and for syntax - there is no 'var' after protected...
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
rhossis

rhossis

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Thanks, I was not aware of the fact that the abstract class could have some features declared in it and inherited.

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
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




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users