gpt4 book ai didi

php - 检查 PHP 类完整性的方法

转载 作者:可可西里 更新时间:2023-11-01 08:44:57 25 4
gpt4 key购买 nike

我正在尝试编写一个脚本来检查我的 PHP 类文件是否与 MySQL 数据库表模式匹配。

数据库表“品牌”

Column       Type          Null    Extra             Key
==============================================================
id INT(11) NO AUTO_INCREMENT Primary
brand_name VARCHAR(50) NO
founded INT(11) NO
status INT(11) NO Default: 1
created_on DATETIME NO
updated_on DATETIME NO

我的 PHP 类文件“Brand.php”如下:

<?php
class Brand {
public $id;
public $brand_name;
public $founded;
public $status;
public $created_on;
public $updated_on;

public function __construct() {
// constructor here
}
}
?>

检查相应属性是否存在的最佳方法是什么?

我想到的一种方法是使用 PHP 脚本来检查上面的 PHP 类:

使用 get_object_vars($obj) 的一个缺点是这个函数只能获取public属性,因为它是在类外调用的。还有更好的选择吗?

由于我使用的是基于Unix的系统,所以使用shell脚本也是可行的。

最佳答案

property_exists()呢? ?

$brand = new Brand();
if(!property_exists($brand, 'brand_name'))
{
throw new Exception("Brand doesn't match table's schema.");
}

关于php - 检查 PHP 类完整性的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31511852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com