473,504 Members | 13,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

call super class, run all subclasses

6 New Member
Hi,

I am looking into a way of adding addons to a class I made.. I thought something like this would work, but not sure how to implement it:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class foo {
  3.     function foobar($text) {
  4.         $text = $text . 'a';
  5.     }
  6. }
  7. class bar extends foo {
  8.     function foobar($text) {
  9.         $text = $text . 'b';
  10.     }
  11. }
  12. ?>
  13.  
Now i want to be able to call up foo, and it calls the foobar of foo, and the foobar of bar.
is that possible?

hosh
Feb 10 '11 #1
9 2681
nathj
938 Recognized Expert Contributor
Hi,
Try using the line
Expand|Select|Wrap|Line Numbers
  1. parent::foobar();
  2.  
in you subclass to call the function on the parent class.

Cheers
nathj
Feb 11 '11 #2
Dormilich
8,658 Recognized Expert Moderator Expert
neither is that possible, nor sensible.

inheritance is meant to work upwards (child classes can access parent classes) not downwards (try calling a child class that doesn’t exist!)

besides that, you are not supposed to call non-static methods statically.
Feb 11 '11 #3
nathj
938 Recognized Expert Contributor
oops, I added the post in a hurry, of course I meant add it to the child class.

As for the static context I was unaware that such concerns existed in PHP. Looks like a bit more reading for me today. Having worked in Java I'm keen to learn that PHP has a proper static context.
Feb 11 '11 #4
Dormilich
8,658 Recognized Expert Moderator Expert
within an object instance of a specified class you have access to all properties/methods of that class and all public and protected properties/methods of all parent classes.

static context is like a global.
Feb 11 '11 #5
nathj
938 Recognized Expert Contributor
Thanks for the pointers. It's great when you start trying to help and then end up learning something more.
Feb 11 '11 #6
superaktieboy
6 New Member
thanks for the answers.. this did the trick for me:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. class lol {
  4.     private $lol = 'HA';
  5.     private $extensions = array();
  6.     function add(ext $obj) {
  7.         $this->extensions[] = $obj;
  8.     }
  9.  
  10.     function loop($text) {
  11.         foreach($this->extensions as $obj) {
  12.             $text = $obj->parse($text);
  13.         }
  14.         return $text;
  15.     }
  16. }
  17.  
  18. abstract class ext {
  19.     public abstract function parse($text);
  20. }
  21.  
  22. class ext1 extends ext {
  23.     public function parse($text) {
  24.         return $text . 'ext1';
  25.     }
  26. }
  27. class ext2 extends ext {
  28.     public function parse($text) {
  29.         return $text . 'ext2';
  30.     }
  31. }
  32.  
  33.  
  34. $lol = new lol();
  35. $lol->add(new ext1());
  36. $lol->add(new ext2());
  37. echo $lol->loop('test');
  38. ?>
  39.  
Feb 11 '11 #7
Dormilich
8,658 Recognized Expert Moderator Expert
a classical Observer Pattern.

I recommend to use interfaces instead of abstract classes, though. (you can only extend one abstract class, but implement many interfaces)

Expand|Select|Wrap|Line Numbers
  1. interface ext
  2. {
  3.     public function parse($text);
  4. }
Expand|Select|Wrap|Line Numbers
  1. class ext1 implements ext
  2. {
  3.     // …
  4. }
tip: class/interface names should begin with an uppercase letter.
Feb 11 '11 #8
nathj
938 Recognized Expert Contributor
That's a neat solution. I appreciate the lesson I've learned here so thanks for that.

Cheers
nathj
Feb 12 '11 #9
Dormilich
8,658 Recognized Expert Moderator Expert
more tips you can find in the Userland Naming Guide
Feb 12 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
9577
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
7
13192
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
5
6506
by: Da Costa Gomez | last post by:
Hi, I was wondering whether someone could shed some light on the following. Using inheritance in Java one can override a function f() (or is it overload?) in the child and then do: public f() {...
1
4138
by: Martin | last post by:
Hi, I've got a base/super class with just a default constructor. I want another constructor, but I can't modify the base class, so I created a sub class with a constructor like so: public...
2
26256
by: Wilson | last post by:
Hi, How can I call the super class in c# ? e.g. protected void Page_Load(object sender, EventArgs e) { call the super class ??? }
4
1709
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
11
18312
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
6
2126
by: jmarcrum | last post by:
Hi everyone! I'm using a super class (DVD.java) that handles another class (EnhancedDVD.java). I want to pass the "details" of the DVD into the super class DVD.java. The super class contains the...
3
2784
by: Marc | last post by:
How would you explicitly call a method of a 'super' class in C#? What do I put at the dots to just call the GetHierarchicalView of XmlDataSource? public class XmlDataSourceMarc : XmlDataSource...
3
2228
by: MiziaQ | last post by:
how can i call a gui component (contentPane) from a super class to a different class ? Code would be appreciated. Thanks in advance
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.