SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing classes : Newbie question

    Hey, Is there any diffrence at all between using
    PHP Code:
    $example->example 
    or
    PHP Code:
    example::example 
    when accessing a class?
    Thanks in advance.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $exampleClassInstance->exampleAttribute is used to access the attributes of an instantiated object.
    PHP Code:
    class myClass
    {
        public 
    $myAttribute 0;

        function 
    __construct($value) {
            
    $this->myAttribute $value;
        }

    }

    $instance1 = new myClass(5);
    $instance2 = new myClass(10);

    echo 
    $instance1->myAttribute;    // will display 5
    echo $instance2->myAttribute;    // will display 10 
    exampleClass::exampleAttribute is used to access an attribute statically, ie. an attribute of the class without the need to instantiate it.
    PHP Code:
    class myClass2
    {
        public static 
    $myAttribute 20;

    }

    echo 
    myClass2::myAttribute;    // will display 20 
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh I see thanks alot!


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
  •