Php ->

Posted on 2012-09-04
Last Modified: 2012-09-04
I'm trying to learn some basics of php, but still do understand when using the (->), like example (is to call a method inside a class?):

$myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );
Question by:rflorencio
    LVL 11

    Accepted Solution

    -> is for an object instantiated, :: for static methods.

    You should take ten minutes to read through this:

    It will give you the basics of OO programming with PHP. In particular, look at Instantiation (Creating objects), Class resolution and scope. It should hopefully get your basic knowledge more complete!
    LVL 11

    Assisted Solution

    An example of the difference between an instantiated operator and static is like this:

    class nonStatic {
    public function hello(){
     return "Hello!"
    class staticclass {
    public static function hello(){
    return "Hello!"
    To call either of these:
    $obj = new nonStatic;
    $output = $obj->hello();
    echo $output;
    $outputStatic = staticclass::hello();
    echo $output;

    Open in new window

    The main difference between calling static and non-static methods is the non-static has functions like __construct that run when the object is first created and can setup variables for use throught the life of that object. Statics dont have that but are quicker to run generally.
    LVL 49

    Assisted Solution

    by:Julian Hansen
    The -> denotes a member of an object

    So if you have
    class MyClass
        // This is a variable local to the class. Each object of type MyClass will have
        // its own version of this variable - called an attribute
        // Here it is public which means it can be accessed outside of the class
        public $name = '';
        // This is a member  function in the class
        function set_name($newname)
            // Here we are using $this which an object uses to refer to itself, to set
            // the member variable name to the newname 
            $this->name = $newname

    Open in new window

    We can now create a new object of type myClass like this
    // Create the class
    $me = new MyClass();
    // Call the function set_name the -> means member of
    // Display the value of this object's internal name variable
    echo $me->name;

    Open in new window

    Your post is basically showing a line that says

    Object: $wpdb is calling an internal function called get_results.

    So in answer to your question - yes it is to call a method in a class or to refer to a member variable (attribute)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now