2014-10-22 47 views
0

我目前有一些問題試圖將2個類庫(1. Form Builder & 2. Custom Emailer)合併到Wordpress插件中。從Wordpress函數訪問PHP對象

背景:

  1. 表單生成器(基於的PFBC http://www.imavex.com/pfbc3.x-php5/index.php的修改版本) - 用來生成用於快速Web開發形式。

  2. 定製Emailer - 爲我們開發的另一個系統提供API的內部庫。

複雜性似乎是在頁腳速度優化中使用wordpress掛鉤輸出頁腳中的javascript輸出。

我的問題是,如何佈局我的代碼,以便我可以從其他函數訪問類。

例如。 echo $ email-> formid();

function wpplugin_blah_forms_init_form() 
{ 
    $email = new DealerSolutionsEmailGateway(); 
    $email->formid = 'blah'; // ID of the <form> 
} 

function wpplugin_blah_forms_show($atts) 
{ 
    // Get Shortcode parameter "form" 
    $forms = shortcode_atts(array('form' => '', 'view' => 'SideBySide'), $atts); 

    // Init EmailProcessor 
    wpplugin_blah_forms_init_form(); 

    echo $email->formid(); 

    $form = new Form("General"); 
    //$form->configure($form_config); 

    $form->addElement(new Element\HTML($theme)); 
    $form->addElement(new Element\HTML('<h2>General Enquiry</h2>')); 
    $form->addElement(new Element\Hidden("form", "General")); 
    $form->addElement(new Element\HTML('<legend>Personal Details</legend>')); 
    $form->addElement(new Element\Button("Submit My Enquiry")); 
    return $form->render(); // display form 
} 
add_shortcode('show_form', 'wpplugin_blah_forms_show'); 

上面的例子是林做,我只是不知道如何當它在另一個功能是啓動訪問$電子郵件的削減版本。

+1

在您的wpplugin中,添加'global $ email;'作爲函數的第一行。 – Jhecht 2014-10-22 03:44:35

回答

0

firstclass.php

<?php 

    class Firstclass{ 
     function __construct(){ 

     } 
     function init(){ 

     } 
     function temp1(){ 

     } 

    } 

    if(class_exists('Firstclass')) 
     $firstclass_object = new Firstclass(); 
?> 

secondclass.php,您可以訪問的firstclass.php功能

<?php 
    class Firstclass{ 
     function __construct(){ 

     } 
     function temp2(){ 
      global $firstclass_object; 
      $firstclass_object-> temp1;  
     }  
    } 
?> 

這裏是你的答案,你可以做如下

//for accessing form id declare it as a global variable 
global $email;  
echo $email->formid;