
內容簡介
安全地呼叫一個函數、類別方法或物件方法,以一種不會因為這些外掛停用而產生錯誤的方式。
提供了各種輔助函數,可以提供此主題的方便變化:
_sfc(): 安全地呼叫一個函數並取得它的回傳值
_sfce(): 安全地呼叫一個函數並回顯它的回傳值
_sfcf(): 安全地呼叫一個函數;如果它不存在,則會呼叫一個備用函數(如果有指定的話)
_sfcm(): 安全地呼叫一個函數;如果它不存在,則會回顯一個訊息(如果提供了)
假設您在模板中有像這樣的代碼:
<?php list_cities( 'Texas', 3 ); ?>
如果停用提供list_cities()的外掛,那麼當訪問該模板時,您的網站將產生錯誤。
相反的,您可以使用這個外掛提供的_sfc()來呼叫其他函數,如下所示:
<?php _sfc( 'list_cities', 'Texas', 3 ); ?>
如果list_cities()函數不可用,這將不做任何事情。
如果您希望在函數不存在時顯示一個訊息,可以使用_sfcm(),如下所示:
<?php _sfcm( 'list_cities', 'The cities listing is temporarily disabled.', 'Texas', 3 ); ?>
在這種情況下,如果list_cities()不可用,將顯示「The cities listing is temporarily disabled.」文本。
如果您希望在函數不存在時調用另一個函數,請改為使用_sfcf(),如下所示:
<?php
function unavailable_function_handler( $function_name ) { echo "The function $function_name is not available."; }
_sfcf( 'nonexistent_function', 'unavailable_function_handler' );
?>
如果要安全地呼叫一個函數並回顯它的值,則可以使用_sfce(),如下所示:
<?php _sfce( 'largest_city', 'Tx' ); ?>
這大致相當於這樣做:
<?php if function_exists( 'largest_city' ) { echo largest_city( 'Tx' ); } ?>
Filter invocation method
為了進一步防止代碼出現問題,如果這個外掛本身被停用了,您可以使用間接的過濾器調用來調用外掛函數。每個函數都有一個與函數同名的過濾器。只需使用apply_filters()調用該函數,而不是直接調用該函數。
例如,不是:
<?php _sfce( 'some_plugin_function_that_echoes', 'argument' ); ?>
而是:
<?php apply_filters( '_sfce', 'some_plugin_function_that_echoes', 'argument' ); ?>
如果您依賴於一個函數的回傳值,並且這個外掛被停用,請注意,apply_filters()調用將返回您打算調用的函數名稱,因此您應檢查返回值以確保函數已被調用。
取代:
<?php $x = _sfc( 'some_plugin_function', 'argument' ); ?>
使用:
<?php
$x = apply_filters( '_sfcq', 'some_plugin_function', 'argument' );
if ( $x !== 'some_plugin_function' ) {
// 在這裡使用 $x 的值。
} else {
// Safe Function Call 外掛沒有啟用。
$x = 0; // 可能將變數設為這種情況下有意義的值。
}
?>
外掛標籤
開發者團隊
📦 歷史版本下載
原文外掛簡介
Safely call a function, class method, or object method in a manner that doesn’t generate errors if those plugins cease to exist.
Various helper functions are provided that provide handy variations of this theme:
_sfc(): Safely call a function and get its return value
_sfce(): Safely call a function and echo its return value
_sfcf(): Safely call a function; if it doesn’t exist, then a fallback function (if specified) is called
_sfcm(): Safely call a function; if it doesn’t exist, then echo a message (if provided)
Let’s assume you had something like this in a template:
If you deactivated the plugin that provided list_cities(), your site would generate an error when that template is accessed.
You can instead use _sfc(), which is provided by this plugin to call other functions, like so:
That will simply do nothing if the list_cities() function is not available.
If you’d rather display a message when the function does not exist, use _sfcm() instead, like so:
In this case, if list_cities() is not available, the text “The cities listing is temporarily disabled.” will be displayed.
If you’d rather call another function when the function does not exist, use _sfcf() instead, like so:
In the event you want to safely call a function and echo its value, you can use _sfce() like so:
Which is roughly equivalent to doing :
Filter invocation method
To further prevent issues in your code should this plugin itself become deactivated, you can use indirect filter invocation to call the plugin functions. Each function has an associated filter with the same name as the function. Simply use apply_filters() to invoke that function instead of calling the function directly.
E.g. instead of:
Do:
If you’re relying on the return value of a function and this plugin gets deactivated, note that the apply_filters() call will return the name of the function you intended to call, so you should check the return value to ensure the function got called.
Instead of:
Do:
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
Developer Documentation
Developer documentation can be found in DEVELOPER-DOCS.md. That documentation covers the template tags and hooks provided by the plugin.
As an overview, these are the template tags provided by the plugin:
_sfc() : Safely call a function and get its return value.
_sfce() : Safely call a function and echo its sanitized return value.
_sfcf() : Safely call a function; if it doesn’t exist, then a fallback function (if specified) is called.
_sfcm() : Safely call a function; if it doesn’t exist, then echo a message (if provided).
These are the hooks provided by the plugin. They are intended for filter invocation usage rather than typical content filtering.
_sfc : Filter to safely invoke _sfc() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
_sfce : Filter to safely invoke _sfce() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
_sfcf : Filter to safely invoke _sfcf() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
_sfcm : Filter to safely invoke _sfcm() in such a way that if the plugin were deactivated or deleted, then your calls to the function won’t cause errors in your site.
