complete n00b question that should be an easy answer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

complete n00b question that should be an easy answer

Post by danwguy »

I see this a lot and have no idea what it means... $somevariable -> something else. What is the -> part of that?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: complete n00b question that should be an easy answer

Post by s992 »

It's referencing a method or a property of an object. For example:

Code: Select all

class SomeClass {
    function SomeMethod() {
        echo "This is a method!";
    }
}

$something = new SomeClass;
$something->SomeMethod(); // Echoes "This is a method!"
Riquez
Forum Newbie
Posts: 10
Joined: Sun Feb 17, 2008 6:39 pm

Re: complete n00b question that should be an easy answer

Post by Riquez »

It is the syntax used in "PHP Classes" which is "Object Orientated Programming".
http://php.net/manual/en/keyword.class.php

Its a more advanced way of programming php in certain situation but very powerful.
I've been using php for about 5+ years & I still don't use classes.
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: complete n00b question that should be an easy answer

Post by mellowman »

Is this way of calling functions similar to the java-scripts concatenating functions?

document.getElementById("Div").innerHTML;
Where the "." is the same as "->"


As well as what is the benefits to using this type of programming?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: complete n00b question that should be an easy answer

Post by superdezign »

Yes, they are essentially the same. The "->" operator also exists in C++ and is for dereferencing pointers. All variables that represent objects in PHP are pointers and the developers used the syntax to denote this.

The largest advantages of class (in my opinion... others may feel differently) are encapsulation and organization. Encapsulation means that the elements and methods are controlled and managed by the class. So, a class (i.e. Database) can have attributes (i.e. username, password, host, etc.) that are unique to any object of that class and controlled by that class. Then, actions (i.e connect, query) can be performed based on input and/or it's own attributes. The organization comes from the separation of duties that object-oriented programming can provide. You can have a class to handle database communication, a class to handle HTML output, a class to handle image uploads, a class to handle form creation and handling, etc.
Post Reply