Dynamic object names

Thread Starter

jakegwood

Joined Apr 13, 2011
29
In any language, (though I am thinking java or PHP), how would I create a dynamic object name? I am fairly new to programming in general and it's something that's been bothering me. So if a user made an account on a website or a ew user on a program, I suppose I could save all the users as an array of objects whose class contains a name variable. But is there a way to actual name the object User. That is a user could enter "John Smith" and a new variable would be created called "John Smith" to which he could input other information and parameters? Or are user objects just all stored in an array of users?

Thanks.
 

someonesdad

Joined Jul 7, 2009
1,583
I don't use java or php, so I can't give any advice there. In a compiled language (e.g., C or C++), the variable names are not relevant at runtime -- they've been removed by the compiler. However, if you wanted to look up the information of someone named "John Smith", you could store the information in a variable (often an array, list, or some other data structure) and use another data structure (e.g. a hash or map) to get to that array entry when given the name "John Smith".

In some interpreted languages, you can define new variables dynamically. However, in a language like python, you'd have to deal with the fact that you couldn't name the variable "John Smith", since variable names are not allowed to contain space characters. But I don't know anyone who would do it that way anyway; I'd use a map to get from the name to the data, assuming the names are unique.
 

Thread Starter

jakegwood

Joined Apr 13, 2011
29
Well, I mean the space wasn't really I was getting at. Let's just assume the user is John. So here is no way that a user could create a new variable "John" and then assign a value to that? It would just have to be an object in an array with a variable in the user class for the name?

So basically my issue with this is how would a website with many users manage an array of millions of objects. I mean I guess you could search through those couple millions, but it seems a bit clunky.
 

kubeek

Joined Sep 20, 2005
5,795
First, AFAIK what you´re getting at is possible in PHP. You should be able to dynamically name new variables, but it will be horribly slow and you will soon run out of RAM, which is typically in the order of 32MB on free hosting servers and if you cross that border execution of that php script will be terminated. Another problem is that you need to perisistently store the data somehow, and serializing PHP objects into a file stored on the disk isn´t fast either.

The industry standard for storing users etc. are databases, with SQL being the most common. Their storage engine is tuned for storing that sort of stuff in tables and doing it fast, so you simply ask the databse to return a row from table "users" where user_name is "John".

In a standard object oriented programming languages like Java and C++, you typically create lots of objects of type "user", and set their inner parameters like name, age, etc. in them. Then you have some sort of "lookup mechanism" which tells you which object has name "John", this is usually done with some form of map of hash table.
 
Last edited:
Top