www.webdeveloper.com
Results 1 to 3 of 3

Thread: Class Confusion: instantiation objects within a constant

  1. #1
    Join Date
    Feb 2005
    Location
    California
    Posts
    147

    Class Confusion: instantiation objects within a constant

    PHP Code:
    class SetOfSearchEngines
    {

         const 
    DEFAULT_ENGINE_LIST = array(
                
    'google' => new GoogleSearch(),
                
    'yahoo' => new YahooSearch(),
                
    'flickr' => new FlickrSearch(),
                
    'blip' => new BlipSearch(),
                
    'jamendo' => new JamendoSearch(),
                
    'spin' => new SpinSearch()
            );



    throws this error:
    Parse error: syntax error, unexpected T_NEW [ . . . ]
    so it looks like maybe i can't instantiate objects in a constant?
    i really just want to set a default value for this "var" $_engineList, which will hold an array of these engine objects. the idea is to have them all in one place so that more can be added later. by making as few edits as possible. perhaps i should re-think my approach?

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Not only can you not use new in that context, but array() will result in a run-time error. Off the top of my head without knowing all the details, I'd guess you'll have to settle for a class variable that is an array of objects, and you'll need to populate it via the constructor.
    PHP Code:
    class SetOfSearchEngines 
    {
       public 
    /* or private? */ $defaultEngineList = array();
       
       public function 
    __construct()
       {
          
    $this->defaultEngineList['google'] = new googleSearch();
          
    // etc....
       
    }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles