如何创建一个静态变量并从另一个类调用它?

时间:2018-05-22 19:30:52

标签: php wordpress static-variables

我有一个名为DB_Bookings的类,在该类中我有一个名为updated_variables()的函数,这是一个简单的脚本,用于查看已发布帖子的日期并相应地更改变量的名称。

这样做,在我的应用程序中,我打算使用变量,它会根据创建的日期动态地更改每个帖子。

我正在努力从另一个类中调用变量。请参阅下面的工作:

class DB_Bookings {

...

public function updated_variables() {
    global $post;
    $compare_date = strtotime( "2018-05-22" );
    $post_date = strtotime( $post->post_date );

    if($compare_date > $post_date) {
        $weddingNameVariable = 'db-bookingsweddingname';

        ...
    } else {
        $weddingNameVariable = 'weddingName';
    }
}

} // end DB_Bookings class

然后在我的另一个类(在一个名为class-db-bookings-admin.php的文件中)

class DB_Bookings_Admin {

...

public function save_metabox( $post_id, $post ) {

...

    update_post_meta( $post_id, DB_Bookings::updated_variables($weddingNameVariable), $db_bookingsnew_weddingname );

...

}

} // end Class DB_Bookings_Admin

这里的想法是我可以回显我的DB_Bookings类中的变量集,它可以根据发布日期进行更改(这实际上是在补偿应用程序的编码时补偿遗留变量)。

但是,它似乎没有保存,而且我收到以下错误

[22-May-2018 19:29:43 UTC] PHP Notice:  Undefined variable: weddingNameVariable in /var/www/html/wp-content/plugins/db-bookings/admin/class-db-bookings-admin.php on line 853

3 个答案:

答案 0 :(得分:0)

我发现你在这里遗失的是声明你的变量并在课堂上将其视为静态。

public static $weddingNameVariable;
if($compare....)
 self::$weddingNameVariable;

这是你想要改变的基本位,但是有些更复杂的一点是不正确的:你将非静态函数视为如果它是静态的。因此,您可能需要将updated_variables函数更改为静态本身。我也看到你在宣布全球$ post后立即尝试$ post-> post_date;但没有初始化它有任何价值。如果您尝试访问从客户端发送的帖子数据,请尝试$ _POST [' some-key-here'],这是由PHP定义并可在任何地方访问。

一旦完全理顺,你可以让你的updated_variables函数返回你设置的新值,或者先调用函数行,然后使用DB_Bookings :: $ weddingNameVariable访问变量。

答案 1 :(得分:0)

我注意到这里有几件事。首先,updated_variables()不是静态方法,尽管您将其称为静态方法DB_Bookings::updated_variables()。要静态使用该方法,您需要通过public static function updated_variables()使其成为静态方法。然而,这本身就是一个讨论。

有许多方法可以实现您想要的效果,但您可以使用全局变量来实现。

<?php

//this is global
$weddingNameVariable = false;

class DB_Bookings {
    public function updated_variables() {
        global $weddingNameVariable;
        //now you can read/update this variable from within this method
    }
}

class DB_Bookings_Admin {
    public function save_metabox( $post_id, $post ) {
        global $weddingNameVariable;
        //now you can read/update this variable from within this method.
    }
}

这可能不是您正在寻找的OOP方法,因为您可以使用静态变量,但如果您需要经常更改该值,我认为您可以通过其他选项更好地管理它。< / p>

答案 2 :(得分:0)

另一种基于评论的方法。

class WeddingVariables {

    //add all of the variables needed to this class
    //you could create getters/setters to manage this data

    $variableA = "data";

    //get the variable
    public function get_variable_a() {
        return $this->variableA;
    }

    //set the variable
    public function set_variable_a( $value ) {
        $this->variableA = $value;
    }
}

//a global variable
$WeddingVariables = new WeddingVariables();

//admin class
class DB_Bookings_Admin {
    public function save_metabox( $post_id, $post ) {
        global $WeddingVariables; //now we can access this within this method

        //get the value of a variable from the class
        $someVariable = $WeddingVariables->get_some_variable();

    }
}
相关问题