3

I need to add functions to my WP_User objects, to be used like this :

<?php
// in my templates :
$user = new User($id);
$user->sendHelloWorldEmail();

// in my functions.php
class User extends WP_User {
    public function sendHelloWorldEmail(){
        // do stuff ...
    }
}

My question is : what is the best way to overload Core Classes (WP_Post/WP_User...) ?

And : is it a good idea, or is there a better way to achieve this (with objects programming) ?

2

What you are doing will work, and is correct as far as the PHP goes.

Whether it is a good idea or not really depends on what you are doing specifically and whether you can do it with hooks-- actions and filters-- instead. If you can do it with hooks you probably should but your question doesn't have detail enough to really allow for a great answer.

I don't think that extending Core classes is "wrong" necessarily but you can set yourself up for higher than normal maintenance as your extended class may need to change if the parent does, so be aware. Its the "fragile base class" problem.

This is not really "overloading", by the way, and in PHP "overloading" is not really that anyway.

| improve this answer | |
  • You're right for the overloading, it's simply inheritance in this case. Here, I'm developing a front-end edition system with multiple post_type, and I'd like to have chlidren classes for each of them, ex : class Product extends WP_Post, where I could do all the validation and Create/Update/Delete. – Vincent Wasteels Aug 19 '13 at 14:45
  • Maybe I should use something like this : wpmvc.org , but I don't like the fact that it's not using the post and post_meta table, and creating it's own table instead... – Vincent Wasteels Aug 19 '13 at 14:51
  • 1
    I have not yet seen a framework that has sold me on its utility. – s_ha_dum Aug 19 '13 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.