Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3622954
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:19:24+00:00 2026-05-18T23:19:24+00:00

I’m currently refactoring the menu class in our PHP CMS, and am currently trying

  • 0

I’m currently refactoring the menu class in our PHP CMS, and am currently trying to work out the best way to handle the issue where someone tries to create a menu (by passing in the title of the menu (we have main, footer, utility etc menus), but the menu isn’t in the database.

If they try to create a menu object with a menu that can be found then there’s no problem, I can return the object as requested. If they try to create one that isn’t found though, I’m currently throwing an exception (which causes an email to be sent), then creating a blank menu object and returning that. The call to output the menu then works without error, but outputs nothing.

(I’ve done the above by setting it up so a static method of the menu class is called to create a menu object, which can then handle throwing an exception if necessary and return either the requested menu object, or a blank one).

Hopefully all that makes sense! Is this the best method to take? Or is there a more elegant solution?

Chris

Edit:

Here’s the static function which is called to create a menu:

static function makeMenu($id,$breakDepth=1){
    // try to create Menu
    try {
        $menu = new Menu($id, $breakDepth);
    }
    catch (no_menu_found_exception $e) {
        // if we failed to find it, an email should have been sent, and create a blank menu so rest of site works without error
        $menu = new Menu("");
    }

    return $menu;
}

and here’s the constructor:

function __construct($id,$breakDepth=1){
    $this->treeObject = Doctrine_Core::getTable('CmsMenuItemNew')->getTree();
    if ($id == "") {
        // if ID supplied is empty, return an empty menu object 
        $this->rootNode = null;
        $this->name = $id;
        return;
    } 
    if (is_numeric($id)) {
        // check it exists?
        $this->rootNode = $id;
        $node = Doctrine_Core::getTable('CmsMenuItemNew')->findByDQL("menuid = '".$id."'")->getFirst();
        $this->name = $node->menutitle;
        if ($this->name == "") $this->rootNode = null;
        return;
    } else {
        $this->name = $id;
        // find the menu ID for the supplied name  
        $table = Doctrine_Core::getTable('CmsMenuItemNew');
        $table->setOption("orderBy", "level");
        $this->rootNode = $table->findByDQL("menutitle = '$id'")->getFirst()->menuid;

        // rootNode with supplied name not found, so look for a branch in the main menu
        $this->breakDepth = $breakDepth;    
        if ($this->rootNode === null) {
            throw new no_menu_found_exception("Menu not found: ".$id);
        }
    }           

}

as mentioned – its still in development, so not completely finished yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:19:25+00:00Added an answer on May 18, 2026 at 11:19 pm

    Building a blank object is a nice thing. The right design pattern for that is called SpecialObjects. To be complete your code should return a MenuNotFound object which have the same interface as MenuObject.
    Then the way this MenuNotFound objects reacts to the interface entry points is up to you.
    This avoid checks on the type of object returned and permits chaining.

    For the Exception I personnaly prefer Exception which are only real problems. But in your case if you want to get a mail the exception is not a bad idea, maybe this exception or mail handling could be done in the MenuNotFound init.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.