Is there a way to get the data from an registration form by counting the inputs and textfields but excluding the submit and hidden ones? Lets say I have a form with 5 input fields that contains the data for registration can I make an array for all so when I process the data instead of:

interface userAction {
    public function login();
    public function logout();
    public function isActive();
    **public function addUser($first_name, $last_name, $email, $password, $username, $company_name, $company_address, $zip_code, $city, $country, $phone, $uid, $userIP, $user_level);**
}

I can do: public function addUser($data); <- $data is assoc array

Dani AI

Generated

Building on 's suggestion to map an incoming array to an object, and addressing 's wish to add fields without rewriting classes: avoid letting form input drive schema or class files directly. Safer, practical patterns are (a) keep a small set of strict properties and store all extra fields in a meta container (or JSON column), (b) use a flexible DTO that holds form data in an internal array with magic accessors, or (c) map incoming keys to existing protected properties at runtime with Reflection and keep unknown keys in a meta array. Those approaches let the DB schema evolve via migrations while handling ad‑hoc fields without brittle code changes.

Example: flexible DTO using magic accessors and an export that whitelists DB fields before saving.

class UserDTO
{
    private $attrs = [];

    public function __construct(array $data = [])
    {
        $this->attrs = $data;
    }

    public function __get($name)
    {
        return array_key_exists($name, $this->attrs) ? $this->attrs[$name] : null;
    }

    public function __set($name, $value)
    {
        $this->attrs[$name] = $value;
    }

    // Use a whitelist when preparing DB params
    public function exportForDb(array $allowed)
    {
        return array_intersect_key($this->attrs, array_flip($allowed));
    }
}

If you prefer strict class properties, use Reflection to set protected/private props and stash unknowns:

$ref = new ReflectionClass($this);
foreach ($payload as $k => $v) {
    if ($ref->hasProperty($k)) {
        $p = $ref->getProperty($k);
        $p->setAccessible(true);
        $p->setValue($this, $v);
    } else {
        $this->meta[$k] = $v;
    }
}

Quick tips: always validate and typecast server‑side, never trust client field names to alter schema, prefer migrations for structural changes, or use a JSON/meta column for unpredictable fields. See PHP overloading and Reflection for examples (get/set, ReflectionClass) and consider MySQL JSON or INFORMATION_SCHEMA for schema introspection (JSON).

Recommended Answers

All 4 Replies

Are you referring to a constructor function that is used when creating new objects of a class? For example:

class User
{
    private $first_name;
    private $last_name;
    private $email;

    public function login() { ... }
    public function logout() { ... }

    public function __construct($data) {
        $this->first_name = $data['first_name'];
        $this->last_name = $data['last_name'];
        $this->email = $data['email'];
    }
}

$myUser = new User($data);
$myUser->login();

Yes something like that I have an interface with user actions and then the classes which implements it such as usercontrol, user, etc.
I did the associative array way, but if I add another field to the registration I want to be able to update the table and the class without making an entire program just for that. There is something that I could for example count the registration fields, retrive the one that has no db table field and automatically update the class for it because the table I got it sorted out to add new field dinamically to it by using a function something like the following:

    // EXAMPLE OF $data array
    // $data = array(
    //     ['name'=>'sql name of the field', 'type'=>'sql data type', 'lungheza/valori'=>'number', 'predefined' => 'sql default'],
    //     ['name'=>'test', 'type'=>'VARCHAR', 'lungheza/valori'=>'255', 'predefined'=>'NULL'],
    //     ['name'=>'test2', 'type'=>'MEDIUMTEXT', 'lungheza/valori'=>'', 'predefined'=>'CURRENT_TIMESTAMP'],
    //     ['name'=>'test3', 'type'=>'INT', 'lungheza/valori'=>'8', 'predefined'=>'UNSIGNED']
    //     );



The only issue that remains is the class

I think that what we do here at DaniWeb is probably close to what you're looking for. Here's a rough example of what we do:

class AllObjects
{

    public function set($properties = array())
    {
        // Loop through properties being passed in
        foreach ($properties AS $property => $value)
        {
            // If property exists for this object type
            if (property_exists($this, $property))
            {                   
                // Update object
                $this->{$property} = $value;
            }
        }   

    }

    /*
     * Constructor function creates a new object of a type that maps to a table in the database
     * and sets the properties for the object
     */
    public function __construct($properties = array())
    {       
        if (!empty($properties))
        {
            $this->set($properties);
        }
    }
}

And then for the specific tables:

class User extends AllObjects
{       
    protected $field1;
    protected $field2;
}

class SomethingElse extends AllObjects
{       
    protected $another_field;
    protected $yet_another_field;
}

Sorry, I forgot to mention how to use it.

With the code above, you can do something like:

$myUser = new User(array(
    'field1' => 'foo',
    'field2' => 'bar',
    'field3' => 'baz'
);

Since class User only has field1 and field2 properties, it will create a new User object with field1 set to foo, and field2 set to bar, and then just silently do nothing with field3 since it doesn't exist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.