无法从 PHP 中的父静态方法访问子静态 属性
Cannot access child static property from parent static method in PHP
我的 PHP 代码有问题。我有三个 classes 命名为
- 数据库对象
- 文件
- user_picture
而他们从父到子的继承树class是DatabseObect->File->user_picture。在 user_picture 中,我有一个静态 属性 $table_name 用于了解每个 [=64] 的数据库 table 名称=] 并且在 DatabaseObject class 中有一些常用的数据库函数。我正在使用的函数代码在这里
数据库对象代码Class
public static function find_by_field($field="", $value="")
{
$result_array = self::find_by_sql("SELECT * FROM ".static::$table_name." WHERE {$field} = '".$value."'"); //Line 52
if(empty($result_array))
{
return false;
}
else
{
$object = static::instantiate(array_shift($result_array));
return $object;
}
}
文件代码 Class
public $errors=array();
public $file_before_upload; // General file tyoe that has been uploaded from the form
public $id = 0;
public $size;
public $file_name;
public $file_type;
public $user_id;
protected $temp_path;
//protected static $upload_dir="";
protected $upload_errors = array(
// http://www.php.net/manual/en/features.file-upload.errors.php
UPLOAD_ERR_OK => "No errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
public static function get_file_by_user($id = 0){
return DatabaseObject::find_by_field("user_id",$id); //Line 164
}
代码 user_picture Class
protected static $table_name = "user_images";
protected static $db_fields = array('id','file_name','file_type','size','user_id');
public static $upload_dir ="uploads".DS."user_img";
protected static $file_upload_limit = 15;
public static function get_image_by_user($id = 0)
{
return File::get_file_by_user($id); //Line 45
}
我在 user_picture 中调用上层函数的地方是
$user_image = user_picture::get_image_by_user($session->user_id);
$user_picture_path="";
if(!$user_image)
{
$user_picture_path = $user_image::upload_dir.DS."default.png";
}
else{
$user_picture_path = $user_image::file_path(); //Line 28
}
此代码出现以下错误
Fatal error: Uncaught Error: Access to undeclared static property: DatabaseObject::$table_name in C:\xampp\htdocs\inventory-management\includes\database\DatabaseObject.php:52
Stack trace:
0 C:\xampp\htdocs\inventory-management\includes\general\File.php(164): DatabaseObject::find_by_field('user_id', '29')
1 C:\xampp\htdocs\inventory-management\includes\user\user_picture.php(45): File::get_file_by_user('29')
2 C:\xampp\htdocs\inventory-management\public\admin\layouts\admin\header\Navigation.php(28): user_picture::get_image_by_user('29')
3 C:\xampp\htdocs\inventory-management\public\admin\layouts\admin\header\admin_header.php(21): require_once('C:\xampp\htdocs...')
4 C:\xampp\htdocs\inventory-management\public\admin\add_page_category.php(44): require_once('C:\xampp\htdocs...')
5 {main}
thrown in C:\xampp\htdocs\inventory-management\includes\database\DatabaseObject.php on line 52
如果有人能帮忙请帮忙!
谢谢,
您必须调用 self
而不是 Database::
和 File::
public static function get_image_by_user($id = 0)
{
return self::get_file_by_user($id); //Line 45
}
public static function get_file_by_user($id = 0){
return self::find_by_field("user_id",$id); //Line 164
}
工作示例:
class A {
static public function foo() {
var_dump(static::$bar);
}
}
class B extends A {
static protected $bar = "12";
static public function zoo(){
self::foo();
}
}
B::zoo();
无效示例:
class A {
static public function foo() {
var_dump(static::$bar);
}
}
class B extends A {
static protected $bar = "12";
static public function zoo(){
A::foo();
}
}
B::zoo();
我的 PHP 代码有问题。我有三个 classes 命名为
- 数据库对象
- 文件
- user_picture
而他们从父到子的继承树class是DatabseObect->File->user_picture。在 user_picture 中,我有一个静态 属性 $table_name 用于了解每个 [=64] 的数据库 table 名称=] 并且在 DatabaseObject class 中有一些常用的数据库函数。我正在使用的函数代码在这里
数据库对象代码Class
public static function find_by_field($field="", $value="")
{
$result_array = self::find_by_sql("SELECT * FROM ".static::$table_name." WHERE {$field} = '".$value."'"); //Line 52
if(empty($result_array))
{
return false;
}
else
{
$object = static::instantiate(array_shift($result_array));
return $object;
}
}
文件代码 Class
public $errors=array();
public $file_before_upload; // General file tyoe that has been uploaded from the form
public $id = 0;
public $size;
public $file_name;
public $file_type;
public $user_id;
protected $temp_path;
//protected static $upload_dir="";
protected $upload_errors = array(
// http://www.php.net/manual/en/features.file-upload.errors.php
UPLOAD_ERR_OK => "No errors.",
UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
UPLOAD_ERR_PARTIAL => "Partial upload.",
UPLOAD_ERR_NO_FILE => "No file.",
UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
);
public static function get_file_by_user($id = 0){
return DatabaseObject::find_by_field("user_id",$id); //Line 164
}
代码 user_picture Class
protected static $table_name = "user_images";
protected static $db_fields = array('id','file_name','file_type','size','user_id');
public static $upload_dir ="uploads".DS."user_img";
protected static $file_upload_limit = 15;
public static function get_image_by_user($id = 0)
{
return File::get_file_by_user($id); //Line 45
}
我在 user_picture 中调用上层函数的地方是
$user_image = user_picture::get_image_by_user($session->user_id);
$user_picture_path="";
if(!$user_image)
{
$user_picture_path = $user_image::upload_dir.DS."default.png";
}
else{
$user_picture_path = $user_image::file_path(); //Line 28
}
此代码出现以下错误
Fatal error: Uncaught Error: Access to undeclared static property: DatabaseObject::$table_name in C:\xampp\htdocs\inventory-management\includes\database\DatabaseObject.php:52 Stack trace:
0 C:\xampp\htdocs\inventory-management\includes\general\File.php(164): DatabaseObject::find_by_field('user_id', '29')
1 C:\xampp\htdocs\inventory-management\includes\user\user_picture.php(45): File::get_file_by_user('29')
2 C:\xampp\htdocs\inventory-management\public\admin\layouts\admin\header\Navigation.php(28): user_picture::get_image_by_user('29')
3 C:\xampp\htdocs\inventory-management\public\admin\layouts\admin\header\admin_header.php(21): require_once('C:\xampp\htdocs...')
4 C:\xampp\htdocs\inventory-management\public\admin\add_page_category.php(44): require_once('C:\xampp\htdocs...')
5 {main} thrown in C:\xampp\htdocs\inventory-management\includes\database\DatabaseObject.php on line 52
如果有人能帮忙请帮忙!
谢谢,
您必须调用 self
而不是 Database::
和 File::
public static function get_image_by_user($id = 0)
{
return self::get_file_by_user($id); //Line 45
}
public static function get_file_by_user($id = 0){
return self::find_by_field("user_id",$id); //Line 164
}
工作示例:
class A {
static public function foo() {
var_dump(static::$bar);
}
}
class B extends A {
static protected $bar = "12";
static public function zoo(){
self::foo();
}
}
B::zoo();
无效示例:
class A {
static public function foo() {
var_dump(static::$bar);
}
}
class B extends A {
static protected $bar = "12";
static public function zoo(){
A::foo();
}
}
B::zoo();