[英]How to create static class constant variable from another constant variable?
[英]How to create a static variable and call it from another class?
我有一个名为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
我所看到的所有您所缺少的就是声明变量并将其在类本身中视为静态。
public static $weddingNameVariable;
if($compare....)
self::$weddingNameVariable;
这是您要更改的基本位,但是还有一些更复杂的位是不正确的:您将非静态函数视为静态。 因此,您可能需要将您的updated_variables函数更改为静态本身。 我还看到您在声明全局$ post之后立即尝试执行$ post-> post_date; 但没有对其进行初始化就没有任何价值。 如果您尝试访问从客户端发送的帖子数据,请尝试$ _POST ['some-key-here'],它由PHP定义并且可以在任何地方访问。
一旦解决了所有问题,您可以让您的updated_variables函数返回您设置的新值,或者在函数之前调用该行,然后使用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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.