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

  • Home
  • SEARCH
  • 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 8289281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:28:20+00:00 2026-06-08T12:28:20+00:00

There are some cases when you override a method which has type hinted input

  • 0

There are some cases when you override a method which has type hinted input parameter like this:

class FooParent
{
    public function bar(BazInterface $baz)
    {
        // ...
    }
}

And you want to allow passing null values as input parameters.

If you remove interface type hint

class Foo extends FooParent
{
    public function bar($baz)
    {
        // ...
    }
}

you’ll get an error like this:

Fatal error: Declaration of Foo::bar() must be compatible with that of FooParent::bar()

How can you allow null values without changing the parent class?

This is a real world example since parent class can be part of the third party library or framework, so changing it is not an option.

  • 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-08T12:28:22+00:00Added an answer on June 8, 2026 at 12:28 pm

    Solution is to add default null value to input parameter like this:

    class Foo extends FooParent
    {
        public function bar(BazInterface $baz = null)
        {
            // ...
        }
    }
    

    This is not what I expected since default value assigns default value to a variable if not provided, I didn’t expect it to affect allowed input. But I saw example on http://php.net/manual/en/language.oop5.typehinting.php, so I decided to document it here. Hope someone will find it useful.

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

Sidebar

Related Questions

Is there some way to define an abstract type as a parameter in an
There are some cases in Java where an inner class extends an outer class.
Are there some good resources tutorials or anyone has tried to implement a Capcha
Is there some crucial difference in writing the registry class in MVC with magic
What's a sound approach to override the CompareTo() method in a custom class with
I have a class that takes an implicit parameter which is used by functions
Is there some kind of software that I can feed uploaded images to and
Is there some way to use Tuning Advisor for SQL Express? Is there some
Is there some event I can use to tell when the SelectedIndices property changes
Is there some regex that can be passed to re-seq such that it will

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.