Page 1 of 1
complete n00b question that should be an easy answer
Posted: Thu Jan 13, 2011 8:49 pm
by danwguy
I see this a lot and have no idea what it means... $somevariable -> something else. What is the -> part of that?
Re: complete n00b question that should be an easy answer
Posted: Thu Jan 13, 2011 8:57 pm
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!"
Re: complete n00b question that should be an easy answer
Posted: Thu Jan 13, 2011 9:00 pm
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.
Re: complete n00b question that should be an easy answer
Posted: Fri Jan 14, 2011 6:18 am
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?
Re: complete n00b question that should be an easy answer
Posted: Fri Jan 14, 2011 9:09 am
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.