PHP Class Integrity Checking Approach

I am trying to write a script to check if my PHP class file matches the MySQL database table schema or not.

Brands database table

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

      

And my PHP class file "Brand.php" looks like this:

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

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

      

What is the best approach for checking the relevant properties, or not?

One approach I am thinking of is using a PHP script to examine the above PHP class:

one drawback of using get_object_vars ($ obj) is that this function can only get public properties as it is called outside the class. Any better alternatives?

Since I am using a Unix based system, using a shell script is also possible.

+3


source to share


1 answer


How about property_exists()

?



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

      

+1


source







All Articles