从Wordpress函数访问PHP对象

时间:2014-10-22 03:41:50

标签: php wordpress class libraries

我目前在尝试将2个类库(1. Form Builder& 2. Custom Emailer)合并到Wordpress插件中时遇到了一些问题。

背景:

  1. 表单构建器(基于PFBC http://www.imavex.com/pfbc3.x-php5/index.php的修改版本) - 用于生成快速Web开发表单。

  2. Custom Emailer - 为我们开发的另一个系统提供API的内部库。

  3. 复杂性似乎是在页脚速度优化中输出javascript,使用wordpress钩子在页脚中输出。

    我的问题是,如何布局我的代码,以便我可以从其他函数访问类。

    例如。 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');
    

    上面的示例是我正在做的事情的简化版本,我只是不确定如何在另一个函数中启动时发送$ email。

1 个答案:

答案 0 :(得分: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;