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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:14:47+00:00 2026-06-09T16:14:47+00:00

If I were writing the below method (for example) is it considered good practice

  • 0

If I were writing the below method (for example) is it considered good practice to either:

A: return an empty string if the document didn’t exist?

B: return a null value?

Having done a lot of Java, and methods in Java requiring a return type, I’m under the impression it is best practice to return a consistent type, is this also the case in PHP or is it better to return a null value instead?

DocumentClass
{
    public function getDir($documentId)
    {
        /* Code to get location of document  */
        return (file_exists($document) ? $document : '');
    }
}

if (!empty($documentClass->getDir(5))
{
    /* Do this */
}

If it is better to return a null value, can you explain why?

  • 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-09T16:14:49+00:00Added an answer on June 9, 2026 at 4:14 pm

    PHP convention

    Return boolean FALSE.

    OOP approch

    Throw an exception. Also you should use a method dir_exitsts (or any other name you like) that only returns boolean (true of false). And use it before calling getDir

    There really no specific rule for this. Its completely up to you.

    I follow PHP way which is returning false.

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

Sidebar

Related Questions

For example let's say I'm writing a method with the below signature (C#4 so
I am writing an AsyncTask as below: class Load extends AsyncTask<String, String, String> {
I constantly find myself writing similar code like the example below: if (object[Object Name]
I've read about new keyword in method signature and have seen the example below
Below is the log of my example app TodoListItemView . I´m sorry for writing
When writing the below my code locks up on GetResponse. Why? try { WebRequest
I am writing the below structure in to the file using Binary Writer. File
In my email regex i want following output abc@abc.co.in I am writing down below
I am new to Regexp. I am struck in writing regexp for below scenario.
I'm writing an sql statement (see below) which compares 2 serial numbers on 2

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.