Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional WordPress Plugin Development
This is the forum to discuss the Wrox book Professional WordPress Plugin Development by Brad Williams, Ozh Richard, Justin Tadlock; ISBN: 978-0-470-91622-3
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional WordPress Plugin Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 7th, 2011, 03:28 PM
Registered User
Points: 10, Level: 1
Points: 10, Level: 1 Points: 10, Level: 1 Points: 10, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: Colorado
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Functions: Call Before Defintiion?

I noticed in the example on page 33 (boj-random-blog-posts.php below) that the function boj_randomly_order_blog_posts is called BEFORE it is defined. The function is defined immediately after the line that invokes it in the add_action hook. I am new to PHP (obviously ), but I do know that in other languages the function must be defined before they are called.

Is this allowed in PHP? If so, what is the best practice regarding this? Is it better to go one way or the other?

Example from book:


<?php

add_action( 'pre_get_posts', 'boj_randomly_order_blog_posts' );

function boj_randomly_order_blog_posts( $query ) {

if ( $query->is_home && empty( $query->query_vars['suppress_filters'] ) )
$query->set( 'orderby', 'rand' );
}

?>
Reply With Quote
  #2 (permalink)  
Old June 15th, 2011, 08:42 AM
Ozh Ozh is offline
Registered User
 
Join Date: Jun 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The example you've copied from the book is best practice and the function is not "called" before it is defined.

First of all, there's nothing wrong in PHP to call a function "before" it is defined. For instance, this works fine:
myfunc_do_something();
function myfunc_do_something() {
echo "I'm doing stuff";
}
The PHP engine first reads and compile files, then execute them.

In the book example, the custom function boj_randomly_order_blog_posts() is not "called": it's associated with an action hook. Basically, it tells WordPress: "hey, when the event 'pre_get_posts' occurs, please execute my function 'function boj_randomly_order_blog_posts', thanks buddy!".

Note also that if you hook a nonexistant function, or a nonexistant hook, no error occurs. In other words:
add_action( 'pre_get_posts', 'xwkwmklm' );
add_action( 'xklmkxmxklm', 'wxcklwmklm' );
add_action( 'ccwxklm', 'a_real_function_that_exists' );
won't break anything, which is nice to know: hooking to a deprecated hook (removed in a future WP version) or relying on a function that's missing (removed in WP or used by a plugin that got deactivated) won't do the expected action, but it won't break your blog with a fatal error.

Finally, I've said in my reply introduction that this is best practice: in WordPress, be sure not to execute functions directly from the plugin, but always hook them into an action. Refer to "Do not check too early" page 119 for more.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto call VBA functions using Java kotaiah Java Basics 2 June 20th, 2007 10:13 AM
Dynamically call javascript functions austinf Javascript 1 May 13th, 2006 07:31 AM
call functions of dll Digon Classic ASP Basics 1 December 13th, 2005 06:12 AM
How to call C# COM overloaded functions from ASP? khaledriyal C# 0 October 11th, 2005 03:13 AM



All times are GMT -4. The time now is 04:03 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.