Please look at the code, the array contains the table field names of the table
class User {
public $db_fields = array('id', 'username', 'password', 'first_name', 'last_name');
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
}
The idea is to remove the public variables with a function so that it automatically creates public variable from the array which i can access —
Example
I want to remove the
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
section, and want this to be automatically generated by the $db_fields array.
So that I can access the objects by
$user = new User();
$user->username = "Ismail";
What I did was
extract($db_fields);
but it gives an error:
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
in C:\xampp\htdocs\advphp\dbclass\extractex.php on line 3
unfortunately your idea does not work
if you try to use
extract($db_fields);it’s a method it need to run from inside a methodsomething like a constructor or a function.
extract($db_fields);it will extract the variables for you but they wont be public they will be local to that function for example if you try thisanother approach is to use a property or setter and getter approach
you can test the codes here http://codepad.org/8MwBwdut