Calling function within same controller – Call to undefined function

Hello!

Feels like I might be doing something wrong here, so please advice me if there’s some better way to do what I’m trying to do.

Thing is I’m drying to break up functionality to smaller functions so readability and understanding for what does what will be clearer.

Basically I want to return posts based on date or custom order, depending on some options I set. So I have a switch that checks what option is chosen and then should run different function loops.

However I’m getting Call to undefined function when trying to call another function from within my controller .

Error message reads Fatal error: Uncaught Error: Call to undefined function App\posts_by_date() in /srv/www/mysite.tld/current/web/app/themes/mytheme/app/controllers/taxonomy-department.php on line *37*

Here’s my code:

<?php

namespace App;

use Sober\Controller\Controller;

class TaxonomyDepartment extends Controller
{
  /**
   * Return layout name for taxonomy page
   *
   * @return array
   */
  public function department_layout() {
    $queried_object = get_queried_object();
    $department_slug = $queried_object->slug;

    $layout = get_field('layout_' . $department_slug, 'option');

    return $layout;
  }


  /**
   * Return posts for taxonomy page
   *
   * @return array
   */
  public function department_posts() {
    $queried_object = get_queried_object();
    $department_slug = $queried_object->slug;

    $order = get_field('order_' . $department_slug, 'option');

    switch ($order) {
      case 'date':
        $posts = posts_by_date();
        break;
      case 'manual':
        $posts = posts_by_custom_order();
        break;
      default:
        $order_is = 'an error has occurred';
    }

    $posts = posts_by_date();

    return $posts;
  }


  /**
   * Load posts by publish date
   *
   * @return array
   */
  public function posts_by_date() {
    $posts = 'hello';

    return $posts;
  }


  /**
   * Load posts by custom order
   *
   * @return array
   */
  public function posts_by_custom_order() {
    $posts = 'goodbye';

    return $posts;
  }
}

Hey @intelligence,

Since you’re calling one class method from another, you need to not only use the method (function) name, but also provide a reference to the class instance.

So, instead of $posts = posts_by_date(); you would do something like $posts = $this->posts_by_date();, where $this refers to the current instance of your controller class.

If you’re unfamiliar with PHP classes / object-oriented programming, I would look up a tutorial or overview and spend a few minutes reading through it–will definitely be helpful for you as you continue your work!

1 Like

Gah. I am familiar with OOP, but seldom use it so I overlooked it.

Thank you for your swift and elaborate reply :slight_smile:

BTW, would you opt for a different route to achieve what I’m trying to do? Say with a static method or a component? Just curious :slight_smile:

I think your current approach is fine. It’s definitely good to break things into smaller methods that don’t try to do too much, and the controller is the right place for this type of thing.

Beyond those principles, just go with what works and if you later run into a limitation caused by your first approach, just refactor it! If you’ve broken things up, you should be able to do that without a massive headache. We often cause more harm than good by worrying too much up front instead of iterating.

This topic was automatically closed after 42 days. New replies are no longer allowed.