WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#6608 closed enhancement (fixed)

get_posts() needs a default argument

Reported by: karlkatzke Owned by:
Milestone: 2.5.1 Priority: low
Severity: normal Version: 2.5
Component: General Keywords: has-patch
Focuses: Cc:

Description

On line 406 of wp-includes/post.php, the function get_posts is specified without a default argument for $args, which means that any time a user calls get_posts without passing arguments in, a PHP warning will be generated -

[Sun Apr 06 00:14:27 2008] [error] [client 74.192.38.89] PHP Warning:  
Missing argument 1 for get_posts(), called in
new.karlkatzke.com/wp-content/themes/noveaukatzke/index.php on line 12 and 
defined in new.karlkatzke.com/wp-includes/post.php on line 406

solution: Redefine the function like function get_posts($args = null) as opposed to function get_posts()

Attachments (1)

6608.diff (418 bytes) - added by Otto42 9 years ago.
Patch for trunk

Download all attachments as: .zip

Change History (9)

#1 @Viper007Bond
9 years ago

  • Type changed from defect to enhancement

Shouldn't users just make sure to add some parameters? Tons of PHP functions will generate an error if you don't pass a parameter to them.

I personally don't think it's our job to make sure people use functions correctly.

But maybe that's just me.

#2 @karlkatzke
9 years ago

  • Type changed from enhancement to defect

When a function's parameters all have defaults, then a user shouldn't always be expected to pass in a parameter when they can just use those defaults.

It's the community's job to write good code with reasonable defaults and to write code that doesn't throw errors when those defaults are used.

#3 follow-up: @Otto42
9 years ago

  • Type changed from defect to enhancement

+1 to making this an optional parameter.

-1 to the notion that this is a "defect". It is an enhancement because it is new functionality, not a correction to existing functionality.

#4 in reply to: ↑ 3 @Viper007Bond
9 years ago

Replying to Otto42:

+1 to making this an optional parameter.

Yeah, I'm not opposed to some defaults either (latest 5 posts from all categories probably) just for ease of use, but this is hardly a defect.

@Otto42
9 years ago

Patch for trunk

#5 @Otto42
9 years ago

  • Keywords has-patch added

#6 @karlkatzke
9 years ago

My point about marking it as a defect:

When you provide no parameters, it will use the defaults already set in the function and then throws a warning.

When you provide some parameters, it works as expected.

When you provide a parameter of null, it uses the defaults already set in the function and does not throw an warning.

Therefore, since it throws a warning/error in it's current state (and hence is a bug) when you use the defaults, it was to me a defect since code should never throw warnings/errors when working properly.

Pedantic, I know. But pedantry is the father of all good code.

Thanks for the patch! Although I would like to note that to bring it into line with the *rest* of wp code it should be $args = '' ... which I didn't notice until I was making sure that we got all of them.

#7 @ryan
9 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [7646]) Make get_posts args optional. Props karlkatzke and Otto42. fixes #6608 for 2.5

#8 @ryan
9 years ago

(In [7647]) Make get_posts args optional. Props karlkatzke and Otto42. fixes #6608 for trunk

Note: See TracTickets for help on using tickets.