Pourquoi utiliser si function_exists?
5 réponses
- votes
-
- 2013-08-23
Vérifier si desfonctions WordPressintégréesexistent avant de les appelerest une compatibilité descendante dont IMHOn'estpasnécessaire.
Donc,si vous voyez
if ( function_exists( 'register_nav_menus' ) )
,l'auteur duthèmeprenden charge les versions antérieures à 3.0.Vous voyezencoreparfois
if ( function_exists( 'dynamic_sidebar' ) )
Pourquoi? Jene pourraispas vous le dire,car dynamic_sidebar a étéintroduit dans la version 2.2.Une autre raison de l'utiliserest de rendre votrethème oupluginenfichable. Unefonctionenfichableest unefonction quipeut être remplacée dans unthèmeenfant ou un autreplugin.
Ceciestfait sur la définitionet non sur l'appelet vous utilisez le! opérateurpour vous assurer qu'iln'existepas déjà avant de le définir.
if ( ! function_exists( 'my_awesome_function' ) ) { /** * My Awesome function is awesome * * @param array $args * @return array */ function my_awesome_function( $args ) { //function stuff return array(); } }
Lorsque celaestfait,unthèmeenfant ou un autrepluginpeut remplacer cettefonctionpar sonpropre.
Checking to see if built in WordPress functions exist before calling them is for backward compatibility which IMHO is not needed.
So if you see
if ( function_exists( 'register_nav_menus' ) )
the theme author is supporting versions earlier than 3.0.You still sometimes see
if ( function_exists( 'dynamic_sidebar' ) )
Why? I couldn't tell you because dynamic_sidebar was introduced in 2.2.Another reason to use it is to make your theme or plugin pluggable. A pluggable function is one that can be overridden in a child theme or another plugin.
This is done on the definition not the call and you use the ! operator to make sure it doesn't already exist before you define it.
if ( ! function_exists( 'my_awesome_function' ) ) { /** * My Awesome function is awesome * * @param array $args * @return array */ function my_awesome_function( $args ) { //function stuff return array(); } }
When this is done a child theme or other plugin can override that function with there own.
-
- 2013-08-23
Lafonction
function_exists
n'estpas un autremoyen de charger lafonction commeadd_action
c'estpour vérifier votre codepour voir qu'iln'y apas d'autrefonction avec lemêmenom doncilne casserapas votre code.Depuisphp.net:Vérifie la liste desfonctions définies,à lafoisintégrées (internes)et définipar l'utilisateur,pournom_fonction.
Si vous avez lamêmefonction deuxfois dans votre code,elle se cassera,c'estpourquoi vouspréfixez Votre fonction avec autre chose que wp_.
En savoirplus: http://php.net/manual/en/function.function-exists.php
The
function_exists
function is not an other way to load the function likeadd_action
its for check your code to see that there are no other function with that same name so it will not break your code. From php.net:Checks the list of defined functions, both built-in (internal) and user-defined, for function_name.
If you have the same function twice in your code it will break, thats why you prefix Your function with something else than wp_.
Read more: http://php.net/manual/en/function.function-exists.php
-
- 2017-02-08
vouspouvez utiliser http://php.net/function_exists
if(function_exists('my_function')){ // my_function is defined }
OU si vous voulez voir lafonction all disponible,donc vousimprimez égalementtout,
echo "<pre>"; print_r(get_defined_functions());
imprimer sur lapageet vouspouvez rechercher cettefonction siellen'estpastrouvée dans la liste signifie qu'ellen'estpas disponible.
vous devrezpeut-être activer l'extensionpour unebibliothèqueparticulière.
you can use http://php.net/function_exists
if(function_exists('my_function')){ // my_function is defined }
OR if you want to see the all function available, so you also print all,
echo "<pre>"; print_r(get_defined_functions());
print on page and you can search that function if it is not found on list means it is not available to use.
you may need to activate extension for particular library.
-
- 2018-03-29
function_exists
doit être utilisé après lenom de lafonction dans unthèmepas avant.add_action( 'loop_start', 'add_slider' ); function add_slider() { if ( function_exists( 'soliloquy' ) ) { soliloquy( 'slider', 'slug' ); } }
Ceci vérifie que lepluginest actif avant de sortir unefonctionet/ou unbalisage sinon vouspourriez obtenir uneerreur comme l'appel à unefonctionnon définie.
function_exists
should be used after the function name in a theme not before.add_action( 'loop_start', 'add_slider' ); function add_slider() { if ( function_exists( 'soliloquy' ) ) { soliloquy( 'slider', 'slug' ); } }
This checks to make sure the plugin is active before outputting a function and/or markup otherwise you might get a error like call to undefined function.
-
- 2019-06-24
php.net définit cela comme
(PHP 4,PHP 5,PHP 7)function_exists - Renvoie TRUE si lafonction donnée a été définie
Je suis d'accord avec Michelle dans son utilisationen ce sens que vous [sh]ne pouvez utiliser la vérification quepour quelque chose de similaire à la vérification de cast detype;vous chercheriez donc si unefonctionexistait (sur cettepage ou àpartir detout ce quiest appelé sur cettepage/référence defichier) afin que vous sachiez s'ilest sûr d'exécuter une autrefonction ou d'analyser éventuellement unmodèle HTML.
Comme quelqu'un l'amentionné,ilest couramment utilisépour vérifier si votrefichier/thème (en utilisant WordPress)est capable d'exécuter la requête dont vous avezbesoin.Je l'ai utilisépour vérifier si unthème a unepartie demodèle spécifique.
if ( function_exists( 'register_sidebar' ) ) { get_sidebar(); }
php.net defines this as
(PHP 4, PHP 5, PHP 7) function_exists — Return TRUE if the given function has been defined
I have to agree with Michelle in usage in that you [sh]ould only use the check for something similar to type cast checking; so you would be looking for if a function existed (on that page or from anything called on that page/file reference) so then you would know if is safe to run another function or parse some template HTML possibly.
As someone mentioned it is used commonly to check if your file/theme (using WordPress) is able to run your needed request. I have used it for checking if a theme has a specific template part.
if ( function_exists( 'register_sidebar' ) ) { get_sidebar(); }
J'ai remarqué que denombreux développeurs dethèmes WordPress utiliseraient ceci dans functions.php-
-
Pourmoi,j'utilisetoujours add_actionpour chaquefonction quej'utilise dans functions.php^
Donc,ce quiprécède serait écrit comme suit:
J'ai deux questions:
Pourquoi utiliser l'instructionif dans lapremièreméthode?
Laquelleest labonnemanière?