【发布时间】:2013-11-06 23:14:55
【问题描述】:
我有一个类似的课程:
class My_Class {
private static $array = null;
private static $another_array = null;
private function __construct() {
self:$another_array = array( 'data' );
}
// This gets executed from jQuery Ajax when user clicks a button
public static function process_ajax() {
self::generate_html();
}
private static function generate_html() {
if ( ! self::$array ) {
self::$array = array( 'some data' );
}
}
// This gets executed when user is trying to save Ajax generated form
public static function save_ajax_form() {
print_r( self::$another_array ); // prints [0] => 'data'
self::validate_data();
}
private static function validate_data() {
// WHY DOES THIS EVALUATE TRUE?
if ( ! is_array( self::$array ) ) {
}
}
}
如何通过 Ajax 调用访问 My_Class::$array 属性?
【问题讨论】:
-
您确定问题出在访问私有属性而不是私有方法上吗?
-
@doublesharp 很好,代码一直执行到 if 语句,我正在检查它是否是一个数组
-
必须查看整个代码才能了解它为什么会这样,但一般来说,私有方法只能在类中调用时才能访问。由于 PHP 是 PHP,您实际上可以从该类的任何实例访问私有方法,在 Java 等语言中,它将是该对象的特定实例。
-
@doublesharp 用更多代码编辑了我的问题
-
您没有调用
generate_html(),所以$array为空,而不是数组。