Php 如何创建静态变量并从另一个类调用它?
我有一个名为DB_Bookings的类,在该类中我有一个名为Php 如何创建静态变量并从另一个类调用它?,php,wordpress,static-variables,Php,Wordpress,Static Variables,我有一个名为DB_Bookings的类,在该类中我有一个名为updated_variables()的函数,其中有一个简单的脚本,用于查看发布文章的日期并相应地更改变量的名称 这样做,在我的整个应用程序中,我打算使用这个变量,它将根据创建的日期在每篇文章中动态变化 我正在努力从另一个类中调用变量。请看下面我的工作: class DB_Bookings { ... public function updated_variables() { global $post; $compa
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
然后在我的另一个类中(在名为classdb bookings admin.php的文件中)
这里的想法是,我可以回显我的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;
这是您想要更改的基本部分,但有一个更复杂的部分是不正确的:您将非静态函数视为静态函数。因此,您可能需要将更新的_variables函数本身更改为静态函数。我还看到,在声明全局$post后,您试图立即执行$post->post\u date;但是没有初始化它以使其具有任何值。如果您试图访问从客户端发送的post数据,请尝试$\u post['some-key-here'],它由PHP定义,可以在任何地方访问
一旦这些都解决了,您可以让更新的变量函数返回您设置的新值,或者在前面一行调用函数,然后使用DB\u Bookings:$weddingNameVariable访问变量。我注意到这里有几件事。首先,
updated\u variables()
不是静态方法,尽管您将其称为静态方法DB\u Bookings::updated\u variables()
。要静态地使用该方法,需要通过公共静态函数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.
}
}
另一种基于评论的方法
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();
}
}
好的,我想我明白了。本质上,抛弃函数,将它们作为全局变量放在我的类之外(要定义的变量大约有30个,而不仅仅是$weddingNameVariable)。那么,我还能在课堂外使用if语句吗?你可以,但我建议不要使用30个全局变量。这将污染全球空间。另一种方法是创建一个管理所有这些的类,并简单地将类本身设置为全局。谢谢@D.Kendall-但是我怎么做呢,比如说,如果我有几个变量(我有大约30个,而不仅仅是weddingNameVariable),那么您肯定可以声明和初始化多个静态变量。或者甚至是一个包含所有30个共享变量的静态变量数组。然而,如果您的项目足够小,可以在单个文件中轻松完成,那么Chris关于使用全局变量的建议可能更容易实现。
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();
}
}