2

The title says it all. I have a class with a lot of variables and a lot of methods in it.

class bootstrap_class { 
public
//-----Counters--------- 
$iColumn_count,
$iLink_count,
$iDia_count,
$iTab_count,
$iPadding,
$iColumns_per_page,

//-----Strings---------
$sCentered, 
$sClass,
etc;

public function a () {
    //do something
}

public function b () {
    //do something
}
}

I am trying to extend this class to create a class without all the methods. I then want to store this new class object into an array variable in the parent class.

Is it possible to extend a class without extending the functions of that class?

PIDZB
  • 903
  • 1
  • 15
  • 38
  • PHP classes conform to basic LSP and subtype polymorphism (this means that a subtype "can do just as much, and usually more" than the parent). It is probably not a good idea to "extend" the class in this case. – user2864740 Dec 07 '15 at 08:42
  • Private methods/params are not available at child classes while protected or public are.. You don't need to reenter them in the childs, they are already there when you extend the parent.. – Svetoslav Dec 07 '15 at 08:45

1 Answers1

3

It is not possible the way you want it. But The following is the only way to do it.

All you have to do is change the methods' visibility to private from public in parent class. See below for details.

what are public private and protected in Object Oriented Programming

Community
  • 1
  • 1
mega6382
  • 9,211
  • 17
  • 48
  • 69
  • So the answer is no... I can't make the functions private, I need them to be public! – PIDZB Dec 07 '15 at 08:48
  • @user2864740 I mean make methods private in parent class. And you are right it is probably public for a reason but it is the only way. – mega6382 Dec 07 '15 at 08:48
  • 1
    Well, I'll be darned. PHP surprises win again. A method defined as `private` in a subclass (when it is `public` in the superclass) will throw an error when accessed. I still wouldn't recommend extending a class if methods are to be "removed", but... – user2864740 Dec 07 '15 at 08:51
  • Oke, @mega6382 I understand it is not possible the way I want it. If you edit your answer to "No" and then the rest as alternative, I will accept it :) – PIDZB Dec 07 '15 at 09:44
  • 1
    Thank you for your explanations! Really appreciate the effort! – PIDZB Dec 07 '15 at 09:47