Wordpress register_activation_hook () + global değişkenler + sınıf problem

1 Cevap php

Benim ilk wordpress eklentisi yazıyorum ve eklenti aktive edildiğinde çağrılacak bir işlev oluşturmak için çalışıyorum.

Şu anda bu gibi görünüyor:

class ThumbsUp {

...

}

global $thumbs;
function thumbs_install() {
    //global $thumbs;
    $thumbs = new ThumbsUp();    /* Line 160 */
    $thumbs->installThumbsUp();
}  /* Line 162 */

// When plugin is activated -> install.
register_activation_hook(__FILE__,'thumbs_install');

Ben eklenti etkinleştirmek Ama ben aşağıdaki hatayı alıyorum:

Plugin could not be activated because it triggered a fatal error.

Fatal error: Cannot redeclare thumbs_install() (previously declared in /dev/site/wp-content/plugins/thumbs-up/thumbs-up.php:160) in /dev/site/wp-content/plugins/thumbs-up/thumbs-up.php on line 162

Ben googled ve baktı ve bir değişken kapsamı sorunu olarak konuştuk oluyor ama ben cevap herhangi bir örnek bulamıyorum ve benim php koduna tartışma çevirmek için yeterince güçlü değildir ettik.

Burada anlatılan çözüm:

Any global variables that you want to reference inside the function
that is called by register_activation_hook() must be explicitly
declared as global inside the main body of the plugin (ie. _outside_
of this function). The plugin file is include()-ed inside another
function at the point where it is activated unlike at others times
when the plugin file is simply include()-ed. Phew. Bit of an odd one
to get your head around but there we go.

Ben tarif ne yapmıştı ama hala hata alıyorum düşündüm. Ben de muhtemelen global $ yaşasın koyabilirsiniz nereye diğer her kombinasyonu denedim ..

Herhangi bir fikir?

Teşekkürler

1 Cevap

Bu soruya daha genel bir cevabı vardır: kayıtlı fonksiyonu çalıştırılır kodunda oluşan her hata register_activation_hook "cannot redeclare ..." yerine gerçek hata olarak gösterilecektir . Ben bu çünkü aktivasyon kanca çağırdığında WordPress eklentisi dosyasını içeren yol olduğunu sanıyorum.