【问题标题】:Accessing PHP Objects from Wordpress Functions从 Wordpress 函数访问 PHP 对象
【发布时间】:2014-12-17 11:04:40
【问题描述】:

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

背景:

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

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

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

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

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

function wpplugin_blah_forms_init_form()
{
    $email = new EmailGateway(); 
    $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 时如何访问它。

【问题讨论】:

  • 在您的 wpplugin 中,将global $email; 添加为函数的第一行。

标签: php wordpress class libraries


【解决方案1】:

对于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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2013-07-17
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多