Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3

Thread: newbie oop

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Location
    Canada
    Posts
    435
    Member #
    8983
    PHP Code:
    <?php
    class announce {
    private 
    $pos 0;
    private 
    $annQuery mysql_query("SELECT * FROM announce WHERE 'ANN_DATE' = '" date("Y-M-d") . "';");
    }
    ?>
    generates:
    Parse error: syntax error, unexpected '(', expecting ',' or ';' in [OMITTED] on line 4
    Likely the most simple mistake just new to oop and trying to learn the ropes
    Thee Pyro Wolf

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    22 times
    You can't have a function call like that when initializing a field in a class. Instead, put it in tha constructor.
    PHP Code:
    class Announce
    {
        private 
    $pos 0;
        private 
    $annQuery null;

        public function 
    __construct()
        {
            
    $this->annQuery mysql_query(...);
        }

    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Here's a more appropriate OO design, using a manager (factory) class and a data "bean" class with simple getters and setters:
    PHP Code:

    class AnnouncementManager {
        public static function 
    retrieveAnnouncementsByDate($date) {
            
    $result mysql_query("SELECT * FROM announce WHERE 'ANN_DATE' = '$date'");
            if (!
    $result) {
                return 
    null;
            } else {
                    
    $announcements = array();
                    while (
    $row mysql_fetch_assoc($result)) {
                        
    $announcements[] = new Announcement($row);
                    }
            }
        }
    }

    class 
    Announcement
    {
        private 
    $id;
        private 
    $title;
        private 
    $message;
        private 
    $otherField;

        public function 
    __construct($fields) {
            
    $this->setId($fields["id"]);
            
    $this->setTitle($fields["title"]);
            
    $this->setMessage($fields["message"]);
            
    $this->setOtherField($fields["other_field_from_database"]);
        }
        public function 
    getId() {
            return 
    $this->id;
        }
        public function 
    setId($value) {
            
    $this->id $value;
        }
        public function 
    getTitle() {
            return 
    $this->title;
        }
        public function 
    setTitle($value) {
            
    $this->title $value;
        }
        public function 
    getMessage() {
            return 
    $this->message;
        }
        public function 
    setMessage($value) {
            
    $this->message $value;
        }
        public function 
    getOtherField() {
            return 
    $this->otherField;
        }
        public function 
    setOtherField($value) {
            
    $this->otherField $value;
        }

    Then you would use the architecture like this.
    PHP Code:
    <?php
        $announcements 
    AnnouncementManager::retrieveAnnouncementsByDatedate() );
        foreach(
    $announcements as $announcement) {
            echo ( 
    $announcement->getId() );
            echo ( 
    $announcement->getMessage() );
        }
    ?>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 07:05 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com