Passing a variable as the name of new objects

Posted on 2007-10-03
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

    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

    Hello paulp75,

    Or use:

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

    This would create a $gallery123456 variable.

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


    LVL 6

    Author Comment

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


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now