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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色