treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Turn Function into a Class

  • 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?
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.