Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Inheritance of properties in base abstract class

inheritance

  • Please log in to reply
5 replies to this topic

#1 rhossis

rhossis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 98 posts
  • Programming Language:Java, PHP, (Visual) Basic
  • Learning:C, C++, Others

Posted 28 January 2011 - 12:41 AM

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
  • 0

#2 ghost_x47

ghost_x47

    CC Regular

  • Just Joined
  • PipPipPip
  • 43 posts

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 -
parent::__construct(args);

Also if you want to be oversure - then you might do some variables checking)
  • 0

#3 sam_l

sam_l

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

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;
  }
}

  • 0

#4 ghost_x47

ghost_x47

    CC Regular

  • Just Joined
  • PipPipPip
  • 43 posts

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)
  • 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;
}
}

  • 0

#5 rhossis

rhossis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 98 posts
  • Programming Language:Java, PHP, (Visual) Basic
  • Learning:C, C++, Others

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.
  • 0

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

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.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.






Also tagged with one or more of these keywords: inheritance