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

How to create a static variable and call it from another class?

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

这样做,在我的整个应用程序中,我打算使用该变量,它会根据创建日期根据 post 动态变化。

我很难从另一个 class 中调用变量。请在下面查看我的工作:

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(在一个名为 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 class 中设置的变量,它可以根据 post 日期进行更改(这实际上是对遗留变量的补偿当我彻底检查应用程序的编码时)。

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

[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

我所看到的是您在这里缺少的是声明您的变量并在 class 本身内将其视为静态变量。

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

这是您要更改的基本位,但有一个更复杂的位是不正确的:您将非静态函数视为静态函数。因此,您可能需要将 updated_variables 函数更改为静态函数。我还看到您在声明 global $post; 后立即尝试执行 $post->post_date;但没有将其初始化为具有任何价值。如果您正在尝试访问从客户端发送的 post 数据,请尝试 $_POST['some-key-here'] ,它由 PHP 定义并且可以在任何地方访问。

一旦这一切都理顺了,你可以让你的 updated_variables 函数 return 你设置的新值,或者调用前面那行的函数,然后使用 DB_Bookings::$weddingNameVariable.

我注意到这里有几件事。首先,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 方法,因为您可以使用静态变量,但如果您需要经常更改该值,我认为您最好通过其他选项来管理它。

另一种基于评论的方法。

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();

    }
}