Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Passing a variable as the name of new objects

Posted on 2007-10-03
Medium Priority
Last Modified: 2008-02-01
I have an object class named Gallery as follows
i would like each of the objects created to be based on a variable
so it would be the name of the object would be $galleryid but trying $$galleryid didnt work,
i would like it to be like this basically
$$galleryid = new Gallery($currentdir)

class Gallery {
function __construct($path)
$this->path = $path;

function getPath()
return $this->path;
private $path;


what i would like to do, is as my script loops through the directories i would like to have each of them create a new object with the name of $nogalleries which is a variable which is set during the running of the script, as mentioned above

thanks for any help
Question by:paulp75
LVL 13

Accepted Solution

MasonWolf earned 2000 total points
ID: 20011682
The first character has to be a letter. Maybe creating an array of objects would work for you?

$gallery_array = array();

$gallery_array[$galleryid] = new Gallery($currentdir);
LVL 49

Expert Comment

ID: 20012068
Hello paulp75,

Or use:

${"gallery".$galleryid} = new Gallery($currentdir);

This would create a $gallery123456 variable.

Alternatively use:
$galleryName = 'gallery'.$galleryid;
$$galleryName = new Gallery($currentdir);



Author Comment

ID: 20012101
thanks roonaan. Still just starting with oop with php, so its a steep learning curve, and thats a great method to use.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question