Results 1 to 11 of 11

Thread: Class

  1. #1
    Join Date
    Feb 2004
    Location
    Klang Bandar Diraja
    Posts
    160
    Rep Power
    199

    Class

    aku try nak apply OOP dlm previous php projek aku... so, skang nih tgh meng'apply' la... aku just tiru je dlm buku.. tp kuar parse error... dah tiru sebijik sebijan dah... okeh aku paste kan aku punya code kat sini....

    -------------------------------user.php----------------------------------------

    <?php
    //define class for tracking users
    class User
    {
    //properties
    public $name;
    private $password, $lastLogin;

    //methods
    public function __construct($name, $password)
    {
    $this->name = $name;
    $this->password = $password;
    $this->lastLogin = time();
    $this->accessess++;

    }

    function getlastLogin()
    {
    return (date("M d Y",$this->lastLogin));
    }

    }
    //create an instance
    $user = new User("Leon","sdf123");
    //get the last login date
    print($user->getlastLogin()."<br>\n");
    //print the user's name
    print ("$user->name<br>\n");

    ?>

    --------------------------------Ouput---------------------------------------

    Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in c:\phpdev\www\try oop\user.php on line 6
    To follow the path:
    Look to the master,
    Follow the master,
    Walk with the master,
    See through the master,
    Become the master.

  2. #2
    Join Date
    Feb 2004
    Location
    Klang Bandar Diraja
    Posts
    160
    Rep Power
    199
    i put away 'public' and 'private' and it works as i want.. but can anyone tell me what exactly happen?
    To follow the path:
    Look to the master,
    Follow the master,
    Walk with the master,
    See through the master,
    Become the master.

  3. #3
    Join Date
    Feb 2003
    Location
    Kluang, Johor
    Posts
    2,367
    Rep Power
    255
    I am not sure where you copy the code. As far as I know, there are other ways to initial private, public and protected variable.
    All private variables and functions always start with underscore "_" followed by lowercase letters like var $_myvar;

    All Protected variables and functions always start with "_T" followed by lowercase letters like var $_Tmyvar;

    All Public variables and functions do not start with underscore "_" like var $myvar;

    All variables and functions always start with lowercase letter (no uppercase) like var $_myvar; and NOT like var $_Myvar;

    PHP Code:
    class someabc {
            var 
    $_conn;                     // Private variable
            
    var $_Tmyvar;                   // Protected variable
            
    var $connMYCONNECTION;          // Public variable
            
    var $connToDb;                  // Public variable
            
    var $myvar3;                    // Public variable
            
    var $myvarTHISTEST;             // Public variable

            
    function _foofunction() {}      // Private function
            
    function _Tfoofunction() {}     // Protected function
            
    function foofunction() {}       // Public function

    Source: http://www.linuxdocs.org/HOWTOs/PHP-HOWTO-7.html

    This explain why the script works after your removed "private", "public". Please note that, all variables in your script have become PUBLIC type.

  4. #4
    Join Date
    Jul 2001
    Location
    ttdi
    Posts
    130
    Rep Power
    229
    actually php4 langsung tak ada support utk public, private or protected access. semua members dan methods automatically jadi public, no matter what.

    tapi dalam php5, OOP features dia lagi lengkap, so public/private/protected access dah di-implement-kan, syntax dia exactly mcm yg pasted:
    PHP Code:
    public $name;
    private 
    $password$lastLogin;
    ...
    public function 
    __construct($name$password)
    { ... } 
    so that example must be a php5 example...

    btw: php5 release candidate 1 is out!

  5. #5
    Join Date
    Feb 2003
    Location
    Kluang, Johor
    Posts
    2,367
    Rep Power
    255
    ya, zaimlah is right. So, nurulmajdi stick with the example I given or upgrade to PHP5.

  6. #6
    Join Date
    Feb 2004
    Location
    Klang Bandar Diraja
    Posts
    160
    Rep Power
    199
    hmm.... i got this from a book which fully revised for php 5. I want to upgrade since i know php 5 RC is released, but i'm using phpdev 423 and it using php 4.2.3 and i don't know which file to altered.
    To follow the path:
    Look to the master,
    Follow the master,
    Walk with the master,
    See through the master,
    Become the master.

  7. #7
    Join Date
    Feb 2003
    Location
    Kluang, Johor
    Posts
    2,367
    Rep Power
    255
    if you use phpdev 423(php 4.2.3) then you cannot use the PHP5 coding. you may either upgrade your PHP version or stick with 4.2.3(do not use the private, public, protected).

  8. #8
    Join Date
    Feb 2004
    Location
    Klang Bandar Diraja
    Posts
    160
    Rep Power
    199
    okeh lcf.. bertindak mengikut arahan!
    To follow the path:
    Look to the master,
    Follow the master,
    Walk with the master,
    See through the master,
    Become the master.

  9. #9
    Join Date
    Nov 2001
    Location
    Johor
    Posts
    772
    Rep Power
    238
    heh upgrade lah...
    clean install satu per satu...
    I hate when:

    vBulletin Message:
    Sorry! The administrator has specified that users can only post one message every 60 seconds

  10. #10
    Join Date
    Feb 2003
    Location
    Kluang, Johor
    Posts
    2,367
    Rep Power
    255
    if you code in PHP5 but your webser use PHP4 then headache lah~
    Since PHP5 is not final release yet, it is advice not to use it for development purposes.

  11. #11
    Join Date
    Nov 2001
    Location
    Johor
    Posts
    772
    Rep Power
    238
    ya ya
    I hate when:

    vBulletin Message:
    Sorry! The administrator has specified that users can only post one message every 60 seconds

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Where is the difference between ID and CLASS?
    By kaisology in forum Website Programming
    Replies: 10
    Last Post: 27-03-2008, 07:36 AM
  2. Spotlight: A class of many languages
    By genzy in forum Mamak Stall
    Replies: 2
    Last Post: 06-07-2007, 02:57 AM
  3. where to learn php ?? (something like tuition class..)
    By Syazwan in forum Website Programming
    Replies: 9
    Last Post: 10-02-2006, 05:49 PM
  4. Multiple database class
    By hajime in forum Website Programming
    Replies: 4
    Last Post: 02-09-2004, 02:58 PM
  5. Review pls.....My Class Website
    By alltvmas in forum Websites Review and Suggestion
    Replies: 12
    Last Post: 20-06-2004, 04:03 AM

Posting Permissions

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

Search Engine Optimization by vBSEO 3.5.0 RC1 PL1

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47