SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Class problem

    Hi,
    I'm pretty new to OOP so will make this quick.

    I have:
    PHP Code:
    class NewDay extends DogHouse{
        private 
    $str date('Y-m-d H:i:s O'); 
    which gives me:
    Parse error: syntax error, unexpected '(', expecting ',' or ';' in class-newday.php on line 1

    Any ideas why?

    Cheers,
    Rhys

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can't initialize a variable using a function.
    PHP Code:
    class NewDay extends DogHouse{
        private 
    $str;
        public function 
    __construct()
        {
            
    $this->str date('Y-m-d H:i:s O'); 

  3. #3
    SitePoint Guru TacMaf's Avatar
    Join Date
    Dec 2005
    Location
    Manchester, United Kingdom
    Posts
    663
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that's what I thought looking at examples. Working great now.

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use magic methods
    PHP Code:
    <?php

    class DogHouse
    {
    }

    class 
    NewDay extends DogHouse
    {
        private 
    $data = array();

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

        public function 
    __get($name)
        {
            if (
    array_key_exists($name$this->data))
            {
                return 
    $this->data[$name];
            }
        }
    }

    $obj = new NewDay;
    echo 
    $obj->date("Y-m-d H:i:s O");
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

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