473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Extending classes at runtime

Hi everybody,

one of the most powerful things about ruby is its ability to add
funcionality to a class (or an instance of a class) at runtime. Can
anyone please tell me if this can be done with php?

thanks a million

Sep 5 '07 #1
7 2227
pt****@gmail.com wrote:
Hi everybody,

one of the most powerful things about ruby is its ability to add
funcionality to a class (or an instance of a class) at runtime. Can
anyone please tell me if this can be done with php?

thanks a million
You could search the manual for "magic methods" and follow the link
"overloading". It may not be what you would do in Ruby, but it should
get you the same possibilities.

Hope this helps,
Dikkie
Sep 6 '07 #2
Dikkie Dik wrote:
pt****@gmail.com wrote:
>Hi everybody,

one of the most powerful things about ruby is its ability to add
funcionality to a class (or an instance of a class) at runtime. Can
anyone please tell me if this can be done with php?

thanks a million
You could search the manual for "magic methods" and follow the link
"overloading". It may not be what you would do in Ruby, but it should
get you the same possibilities.
Sorry to follow up on my own post, but you can combine these techniques
with "callback functions". You might want to take a look on that topic also.
Sep 6 '07 #3
C.
On 6 Sep, 09:59, Dikkie Dik <dik...@nospam.orgwrote:
Dikkie Dik wrote:
ptd...@gmail.com wrote:
Hi everybody,
one of the most powerful things about ruby is its ability to add
funcionality to a class (or an instance of a class) at runtime. Can
anyone please tell me if this can be done with php?
thanks a million
You could search the manual for "magic methods" and follow the link
"overloading". It may not be what you would do in Ruby, but it should
get you the same possibilities.

Sorry to follow up on my own post, but you can combine these techniques
with "callback functions". You might want to take a look on that topic also.
You could create a string with a new sub-class and eval() it ...

$classname='revised';
$newdef=' class ' . $classname . ' extends baseClass {
function extension() { print "Hello world\n"; }
}'
eval($newdef);
$obj=new $classname();

(not tested)
but I find it difficult to see any value in doing this at runtime -
and it opens up a attack vector.

C.

Sep 6 '07 #4
the idea is that in a mvc-framework-like application, new modules
(controllers) interact (by adding or redefining) the behavior
(methods, actions) of existing ones. each part of the system should be
able to reach and mess each other (if needed).

for instance, an audit module. you dont need it in every site but in
some its essential. so when you install that module, it should be able
to attach himself to some modules (you may choose which ones in the
admin settings) and from them on every time some method in some
controller is called, the audit makes sure that that action, user,
date and data is tracked.

I found an implementation of this first in drupal with the
hook_nodeapi. that callback allowed new modules to add new fields at
runtime to certain types of nodes. but the implementation in ruby was
much cleaner. the same for js (with the prototype) and, if im not
wrong, java aop (aspect-oriented programming) achieves the same.

thanks Dikkie for pointing the __call method together with a callback
though i need to think better on this.

Sep 6 '07 #5
On Sep 6, 9:14 am, ptd...@gmail.com wrote:
the idea is that in a mvc-framework-like application, new modules
(controllers) interact (by adding or redefining) the behavior
(methods, actions) of existing ones. each part of the system should be
able to reach and mess each other (if needed).

for instance, an audit module. you dont need it in every site but in
some its essential. so when you install that module, it should be able
to attach himself to some modules (you may choose which ones in the
admin settings) and from them on every time some method in some
controller is called, the audit makes sure that that action, user,
date and data is tracked.

I found an implementation of this first in drupal with the
hook_nodeapi. that callback allowed new modules to add new fields at
runtime to certain types of nodes. but the implementation in ruby was
much cleaner. the same for js (with the prototype) and, if im not
wrong, java aop (aspect-oriented programming) achieves the same.

thanks Dikkie for pointing the __call method together with a callback
though i need to think better on this.
Look at this:

<http://www.php.net/runkit>

Sep 6 '07 #6
thanks, that the whole point: add, rename, redefine, remove methods at
runtime.

but somehow im not too fond of pecl. i'd rather see those kind of
features implemented in pure php code.

Sep 6 '07 #7
On Thu, 06 Sep 2007 15:14:54 +0200, <pt****@gmail.comwrote:
the idea is that in a mvc-framework-like application, new modules
(controllers) interact (by adding or redefining) the behavior
(methods, actions) of existing ones. each part of the system should be
able to reach and mess each other (if needed).

for instance, an audit module. you dont need it in every site but in
some its essential. so when you install that module, it should be able
to attach himself to some modules (you may choose which ones in the
admin settings) and from them on every time some method in some
controller is called, the audit makes sure that that action, user,
date and data is tracked.
Sounds more like an Observer pattern is needed? No screwing with classes,
just enforce an 'observable' interface unto classes in need of observing,
and add the right Observers to it?
--
Rik Wasmus
Sep 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: needin4mation | last post by:
Hi, I read this in a book about the Xml classes in c#: "These classes are abstract and therefore must be extended." I just wanted to know what this statement means. I know it is not in...
1
by: Sarge | last post by:
Hi all, tough question. Apologies for the cross posting but it is an interesting architectural problem and I think deserves a wide audience. What is the best way to extend web service proxy...
3
by: James | last post by:
Hi, I've been trying to figure this out for a while and can't find the answer. For simplicity I will use a basic example rather than my real classes. I have one base class, Item and various...
9
by: Fat Elvis | last post by:
I'd like to extend some of my Asp.net pages by using Partial Classes. Example ASP.Net Page: public partial class Admin_Customer : System.Web.UI.Page { protected void Page_Load(object sender,...
13
by: interec | last post by:
I have some code in Java that I need to translate into C++. My Java code defines a class hierarchy as follows: // interface IA public interface IA { public abstract void doA(); } //...
21
by: Daz | last post by:
Hi everyone. I am trying to create an extension of the mysqli class within PHP, and I am finding it quite difficult. I am fairly new to PHP classes, and decided to give them a go. Here's what I...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
5
by: Wolfgang Hanke | last post by:
Hello, I want to extend multiple Controls like TextBox, Label, ComboBox etc. with the same new featureset (for Example all need a Method getSomething()) Because I cant alter their Base-Class I...
8
by: Floortje | last post by:
Hi i have been struggeling with this question for quite some time now. I have some helper classes that handle images (upload an image, create thumbnails and show a imagelist), links (add link,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.