dshw124502 2014-10-22 03:41
浏览 53
已采纳

从Wordpress函数访问PHP对象

I'm currently having some issues trying to incorporate 2 Class Libraries (1. Form Builder & 2. Custom Emailer) into a Wordpress plugin.

Background:

  1. Form Builder (based of a modified version of PFBC http://www.imavex.com/pfbc3.x-php5/index.php) - Using to Generate Forms for Rapid Web Development.

  2. Custom Emailer - In-house library that provides an API to another system we've developed.

The complexity seems to be outputting the javascript in the footer for page speed optimisation, using wordpress hooks to output in the footer.

My question is, how do layout my code so I can access Classes from other functions.

eg. 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');

The above example is a cut down version of what im doing, im just not sure how to access $email when it was initiated in another function.

  • 写回答

1条回答 默认 最新

  • dousigan0499 2014-10-22 03:53
    关注

    for firstclass.php

    <?php
    
        class Firstclass{
            function __construct(){
    
            }
            function init(){
    
            }
            function temp1(){
    
            }
    
        }
    
        if(class_exists('Firstclass'))
            $firstclass_object = new Firstclass();
    ?>
    

    In secondclass.php, you can access functions of firstclass.php as

    <?php
        class Firstclass{
            function __construct(){
    
            }
            function temp2(){
                global $firstclass_object;
                $firstclass_object-> temp1;       
            }    
        }
    ?>
    

    Here for your answer, you can do that as below

    //for accessing form id declare it as a global variable
    global $email;    
    echo $email->formid;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料