-5

am newer to php i find lot's of code given symbol like (->)

for ex:

 $st = $db->prepare("SELECT * FROM jqm_categories");

here getting some values from table jqm_categories and global $db; it was database config file given below

/*
    This file creates a new MySQL connection using the PDO class.
    The login details are taken from includes/config.php.
*/

try {
    $db = new PDO(
        "mysql:host=$db_host;dbname=$db_name;charset=UTF-8",
        $db_user,
        $db_pass
    );

    $db->query("SET NAMES 'utf8'");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
    error_log($e->getMessage());
    die("A database error was encountered");
}

Here why they used "->" symbol

STT LCU
  • 4,348
  • 4
  • 29
  • 47
pd1786
  • 15
  • 6

4 Answers4

1

the official name for this sign is object operator. this sign accesses a member of an object. So $wp_query->no_of_pages is accessing the field no_of_pages in the object $wp_query. It can be used to access either a method or a field belonging to an object. in C++ or Java, it's equivalent to myObject.myField

for more understanding, you can refer here.

neshpro9
  • 433
  • 3
  • 17
0

This is the way to access to a property from a object.

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property. See Static Keyword for more information on the difference between static and non-static properties.

http://php.net/manual/en/language.oop5.properties.php

mcuadros
  • 4,098
  • 3
  • 37
  • 44
0

PHP is an object-oriented language, which means that variables can be declared as a particular "type", and can be thought of as "objects". This is an extreme simplification, but that -> arrow operator is a way for an object to access the data that belongs to it.

So if you had a class called Person:

class Person
{
    public $firstName;
    public $lastName;
}

and you created a new Person object and called it $someone, then you'd be able to get that Person's last name by calling $someone->lastName;

There a lot more to it so I suggest reading up on it in the PHP documentation:

Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
  • thanks and one more question in mvc based web development view folder consist _home.php like the using _before file name what that ? – pd1786 Nov 27 '13 at 09:18
0

"->" is called as object operator or arrow in normal language. It is used to access an object..

In short:

It is equivalent to .(DOT) notation used in java. just like . in System.out.println("hello");

s.mujahid8
  • 189
  • 3
  • 6
  • 15