Grow your CSS skills. Land your dream job.

Turn Function into a Class

  • # May 31, 2012 at 3:54 pm

    I’m trying to turn this function into a reusable class.


    function more_jump_link($link) {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }
    add_filter('the_content_more_link', 'more_jump_link');

    This is what I’ve come up with but it doesn’t work.


    class wp_more_jump_class {

    function __construct() {
    $functions_array = array(
    $this->more_jump_link($link)
    );
    return $functions_array;
    }


    public function more_jump_link() {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }


    public function action_filters (){
    add_filter('the_content_more_link', $this->more_jump_link);
    }
    }

    $call_wp_more_jump = new wp_more_jump_class();
    $call_wp_more_jump->more_jump_link();

    I not sure why it doesn’t work I’ve made functions int class like this before and they worked.

    Anyone have any ideas on this?

    # May 31, 2012 at 5:33 pm

    What’s the problem exactly?

    From what I’ve seen so far, though :

    function __construct() {
    $functions_array = array(
    $this->more_jump_link($link)
    );
    return $functions_array;
    }

    You put $link in more_jump_link, however you don’t pass it in your constructor, and you don’t have a set method. $link will then always be null. $link is not linked to any property in your class.

    # May 31, 2012 at 5:38 pm

    I’m not sure it just doesn’t work. Now If I have this.

    public function remove_more_jump_link($enable_rmvMoreJump) {
    function more_jump_link($link) {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }
    add_filter('the_content_more_link', 'more_jump_link');
    }

    It works just fine. I just don’t like having a function in a function like that. I’m trying to clean up my code, but I can’t get it to work.

    # May 31, 2012 at 5:40 pm

    See, in your function, you’re passing ($link) in the parameters of the function. However you never do in your class, thus $link is always null in the code.

    Also, in the constructor, you are doing this :

    function __construct() {
    $functions_array = array(
    $this->more_jump_link($link)
    );
    return $functions_array;

    $link at this point is null, since you did not receive it in construct($link). Also, your more_jump_link function is defined as not receiving any parameter :


    public function more_jump_link() {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }

    You can see there that jump_link() is not waiting for the $link parameter you are passing in the constructor.

    Do something like this and tell me if it work :


    class wp_more_jump_class {
    function __construct()
    {
    }

    public function more_jump_link($link) {
    $offset = strpos($link, '#more-');
    if ($offset) {
    $end = strpos($link, '"',$offset);
    }
    if ($end) {
    $link = substr_replace($link, '', $offset, $end-$offset);
    }
    return $link;
    }

    public function action_filters ()
    {
    add_filter('the_content_more_link', $this->more_jump_link);
    }
    }

    $call_wp_more_jump = new wp_more_jump_class();
    $call_wp_more_jump->more_jump_link($link);

    Note : action_filters will crash if you call it. You are trying to access a variable that does not exist.

    # May 31, 2012 at 6:07 pm

    Yeah, that doesn’t work. I’ve already been down that road. I think I’m just going to go back to using it as a plain old function. This is taking up too much of my time. I was just trying to work some OOP into my code.

    Thanks for your help anyways.

    # June 1, 2012 at 1:44 pm

    In and of itself, the function in your original post should be a function, and not a class.

    It receives a value, does something simple to it, returns it, and forgets about it. Unless this is just one step in a bigger process, then there is no point in turning it into a class.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".