[PHP] A small question about OOP

Results 1 to 9 of 9
  1. #1
    Web Developer Markshall is offline
    Grand MasterRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    [PHP] A small question about OOP

    I've seen a few codes of OOP in PHP and wondered the same thing about them all.

    In some codes you see something like this:

    PHP Code:
    $some_random_thing $class::SOMETHINGHERE
    And then you might see:

    PHP Code:
    $some_random_thing $class->SOMETHINGHERE
    What is the difference between them? I think the top example is a static or something? I'm not entirely sure.

    It's not important - I just want to know the difference between them.


  2. #2
    1/11/1995 ~ 23/11/2011 rebora2007 is offline
    Grand MasterRank
    Nov 2010 Join Date
    Care much ?Location
    2,346Posts

    Re: [PHP] A small question about OOP

    -> is used when referring to a member of an object.

    :: is the Scope Resolution Operator and is used to refer to a static member of a Class.
    If that's PHP ofc, in C++ -> allows you to transfer a variable of a pointer.

  3. #3
    Sorcerer Supreme Hexadecimal is offline
    Member +Rank
    Dec 2010 Join Date
    424Posts

    Re: [PHP] A small question about OOP

    $Variable->Function

    That usually tells signs that the instance was created under the name $Variable.

    Such as;

    PHP Code:
    $Variable = new Class();

    $Variable->Function(); 
    On the other hand, Variable::Function(); indicates that the function is static. To access a static function, the instance doesn't have to be created. The function can be called.

  4. #4
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP] A small question about OOP

    Code:
    ->
    is the standard way to use OOP verus PHP: Scope Resolution Operator (::) - Manual
    Last edited by holthelper; 27-10-11 at 07:49 PM.

  5. #5
    Garry's Mod is addictive! Law is offline
    Grand MasterRank
    Dec 2009 Join Date
    NorwayLocation
    993Posts

    Re: [PHP] A small question about OOP

    Quote Originally Posted by m0nsta. View Post
    I've seen a few codes of OOP in PHP and wondered the same thing about them all.

    In some codes you see something like this:

    PHP Code:
    $some_random_thing $class::SOMETHINGHERE
    And then you might see:

    PHP Code:
    $some_random_thing $class->SOMETHINGHERE
    What is the difference between them? I think the top example is a static or something? I'm not entirely sure.

    It's not important - I just want to know the difference between them.
    s-p-n explained this to me, and he said class::SomeFunction(blabla, blabla); is static and some shit about the other,

    when your going to do -> you have to do $class = new Class(); right, but when you are using Class::SomeFunction(bla, bla); you won't be needing this.

    I cannot remember which you should use where, but that's just to read about somewhere on the internet.

  6. #6
    Web Developer Markshall is offline
    Grand MasterRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    Re: [PHP] A small question about OOP

    Sorry I was aware that people had replied to this thread - I forgot I had even posted it. Anyway, thanks for the help. I think I see where you're coming from now.

  7. #7
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP] A small question about OOP

    Quote Originally Posted by Law View Post
    s-p-n explained this to me, and he said class::SomeFunction(blabla, blabla); is static and some shit about the other,

    when your going to do -> you have to do $class = new Class(); right, but when you are using Class::SomeFunction(bla, bla); you won't be needing this.

    I cannot remember which you should use where, but that's just to read about somewhere on the internet.
    please have a look at the link i put in my post, it will help refresh your memory.

  8. #8
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,097Posts

    Re: [PHP] A small question about OOP

    Quote Originally Posted by holthelper View Post
    Code:
    ->
    is the standard way to use OOP verus PHP: Scope Resolution Operator (::) - Manual
    Slight correction, "->" is the most common way, not standard. People are afraid of the unknown. There are really good uses for the "Paamayim Nekudotayim" operator. Static classes in general have many good uses. Limiting yourself to one slim style of OOP is not what OOP was created for. Use the robustness to your advantage and think outside of the "common knowledge" box- That's how these great patterns get discovered (or invented? Idk).

    Don't let the name scare you- try the link Holthelper put, and always look up things you aren't sure about.

  9. #9
    Elite Member KrYpT0n is offline
    Member +Rank
    Dec 2010 Join Date
    MasqueradeLocation
    205Posts

    Re: [PHP] A small question about OOP

    & = address of(reference operator)
    */-> = value pointed by(dereference operator)
    for example:
    Code:
    (*ptr).method(); // Both of these are the same
    ptr->method();   // When the operator is not overloaded
    :: - static dereference operator
    for example
    Code:
    int count = 5;
    int main()
    {
    int count = 7;
    printf( "%d - %d", /* local variable */count, /*global variable*/::count );
    }
    It basically works the same way in php, and i'm really not sure how else i could explain it ;S



Advertisement