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 8852537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:25:36+00:00 2026-06-14T13:25:36+00:00

I have a method / function (in a class) that I am calling and

  • 0

I have a method / function (in a class) that I am calling and I want to pass either and id or a url. The method then uses one of these arguments in a mysql WHERE query. It can only use one or the other.

How can I require one of two arguments? Or in other words how can I only make one of two arguments optional?

Is there a better way than to just make both arguments optional and use several if else statements?

  • 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-06-14T13:25:37+00:00Added an answer on June 14, 2026 at 1:25 pm

    If you want to literally require an argument:

    public function fetchRow($where)
    {
        if (empty($where['id']) && empty($where['url'])) {
            throw new Exception('WHERE clause must be supplied');
        }
    
        // proxy to specific method
        if (!empty($where['id'])) {
            return $this->fetchById((int) $where['id']);
        } elseif (!empty($url)) {
            return $this->fetchByUrl((string) $where['url']);
        }
    }
    
    public function fetchById($id) 
    {
        if ($stmt = $mysqli->prepare('SELECT * FROM table WHERE id = ?')) {
            $stmt->bind_param('i', $id);
            // ...
        }
    }
    
    // ...
    $object = new MyClass();
    try {
        $object->fetchRow(); // would throw exception
        $object->fetchRow(array('id' => 10)); // would work
    } catch (Exception $ex) {
        // do something, for example:
        echo $ex->getMessage(); // echoes 'at least one argument must be supplied'
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that I want to call from within a class method.
I have one method that opens a file and passes off to another function
I have a method that is a pure function and takes a while to
I want to have a java app running, using a function/method (with as little
Eg. I have following delegate method I want to use as a callback function
I have a function that use setInterval() method to watch css propertie change('cause in
I have a function that needs to call a virtual method many times in
I have a logging function that takes the calling object as a parameter. I
I have a .php that registers a user by calling another php function but
I have a question involving calling a class's generic method with a type parameter

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.