I have this code and wonder get the methods properties values.
<?php
class framework{
// users table
function users(){
$username="VARCHAR (255)";
$email="VARCHAR (255)";
$password="VARCHAR (255)";
}
// product table
function produts(){
$title="VARCHAR (255)";
$price="INT ()";
$author="VARCHAR (255)";
}
//categories table
function categories(){
$category_name="VARCHAR (255)";
}
}
?>
I'm trying to create my own framework and in this particular code I'm trying to simplify database creation. The idea is to get the class name as the database name and the methods as tables names, and the last get the methods properties as cols.
I use get_class() to get the class name and add as table name;
get_class_methods to get the class methods and create the table.
So I have no idea of how to create the respective cols.
My Answer:
You can use the get_class_methods() function to get an array of method names within the class. Then, you can use the ReflectionClass class to get the properties of each method and their values.
Here's an example of how you can achieve this:
php
class framework{
// users table
function users(){
$username="VARCHAR (255)";
$email="VARCHAR (255)";
$password="VARCHAR (255)";
}
// product table
function products(){
$title="VARCHAR (255)";
$price="INT ()";
$author="VARCHAR (255)";
}
//categories table
function categories(){
$category_name="VARCHAR (255)";
}
}
$framework = new framework();
$class_name = get_class($framework);
$methods = get_class_methods($class_name);
foreach($methods as $method){
$reflectionMethod = new ReflectionMethod($class_name, $method);
$properties = $reflectionMethod->getStaticVariables();
echo "Table name: " . $method . PHP_EOL;
foreach($properties as $property => $value){
echo "Column name: " . $property . ", Value: " . $value . PHP_EOL;
}
echo PHP_EOL;
}
?>
This code will output the table name (method name) and the column names (properties) with their respective values for each method in the class.
Rate this post
5 of 5 based on 5552 votesComments