Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9015099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:40:01+00:00 2026-06-16T03:40:01+00:00

Please look at the code, the array contains the table field names of the

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:40:02+00:00Added an answer on June 16, 2026 at 3:40 am

    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 method
    something 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 this

    function __construct(){
        extract($db_fields);
        // the $id will be available in the constructor only
        // it will get disposed when this method finished executing
    }
    

    another approach is to use a property or setter and getter approach

    <?php
    class User {
        private $db_fields = array(
                            'id', 
                            'username', 
                            'password' => 'ismailPassword', 
                            'first_name',
                            'last_name'
          );
    
        function getValue($key){
           if (array_key_exists($key, $this->db_fields)){
                return $this->db_fields[$key];
           }
           return NULL;
        }
    
        function setValue($key, $value){
            $this->db_fields[$key] = $value;        
        }
    }
    
    $user = new User();
    $user->setValue('username', 'Ismail');
    echo " Username: ";
    echo $user->getValue('username');
    echo "\n\n Password: ";
    echo $user->getValue('password');
    ?>
    

    you can test the codes here http://codepad.org/8MwBwdut

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Look please at this code: public static void main(String[] args) { String[] array =
Please look at this code: def chop(array, search): lo = 0 high = len(array)
Please take a look at this code: template<class T> class A { class base
Look please below this codes throw me : FormatException was unhandled by user code
Please have a look at the following code public List searchData(String query) { List
Please look at this code: @interface myObject:NSObject -(void)function:(id)param; @end @implementation myObject -(void)function:(id)param { NSLog(@BEFORE);
Please look at the following code: char* test ( ) { char word[20]; printf
Please look at the below code snippet and let me know how the out
Getting the above error in following code. How to rectify it. Thanks. Please look
am new here. i have a slight problem; PLease look at the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.