What is this syntax?

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
pchika
Forum Newbie
Posts: 3
Joined: Thu Sep 24, 2009 11:18 am

What is this syntax?

Post by pchika »

Hi everybody,

A php newb here, with what I'm sure will seem like a ridiculously simple question but it's new to me so here goes. I've been a programmer for over 15 years, but I've never seen the following syntax in any other language. I don't really understand what it means.

Here is an example:
$x=$xmlDoc->getElementsByTagName('ARTIST');

Here's another:
$cd=($y->childNodes);

And another:
for ($i=0;$i<$cd->length;$i++)


That little bit that kind of looks like a cheap asciii attempt to make a rightwards pointing arrow, "->" what is that? Minus less than? It doesn't really make sense to me. I've checked dozens of online tutorials, websites, and fact sheets. Some use the syntax but none I've found actually explains it. They just present it and assume we know what it means. It's not that intuitive to me. I've worked in a lot of different languages in my career but none that uses this syntax. So it's not that intuitive to me. Maybe I'm just thick, or coming from a different angle than most. SQL Server, Javascript, VB. Never been much up on the C derivitivs. Maybe it's used there, but not in anything I've worked in.

Any help, greatly appreciated.

Thanks,
Phil
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: What is this syntax?

Post by ricehigh »

It's basic OOP (Object Oriented Programming)

The thing you see before the "->" is the object, and what comes after is what you're trying to fetch inside the object.
For instance $object->Names, here you're accessing the object called $object and trying to fetch the variable, funciton or likewise inside the object calles "Names".

If you're interested you could look more into OOP on the php website: http://dk.php.net/manual/en/language.oop5.php
pchika
Forum Newbie
Posts: 3
Joined: Thu Sep 24, 2009 11:18 am

Re: What is this syntax?

Post by pchika »

Sort of like dot notation then? For example in VB or javascript it would be object.property or object.method? So in PHP we do with with object->property using [->] instead of a [.]?
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: What is this syntax?

Post by watson516 »

pchika wrote:Sort of like dot notation then? For example in VB or javascript it would be object.property or object.method? So in PHP we do with with object->property using [->] instead of a [.]?
Yes.
pchika
Forum Newbie
Posts: 3
Joined: Thu Sep 24, 2009 11:18 am

Re: What is this syntax?

Post by pchika »

Got it, makes perfect sense. Thanks to all!!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: What is this syntax?

Post by jackpf »

Sort of like dot notation then? For example in VB or javascript it would be object.property or object.method?
Or like C++'s object pointers :)

Like:

Code: Select all

#include <iostream>
#include <conio.h>
 
using namespace std;
 
class foo
{
    public:
        void bar()
        {
            cout << "Hello world." << endl;
        }
};
 
int main()
{
    foo *foo;
    foo->bar();
 
    getch();
    return 0;
}
Would be the same as

Code: Select all

<?php
class foo
{
    public
        function bar()
        {
            echo 'Hello world.';
        }
}
 
$foo = new foo;
$foo->bar();
 
return 0;
?>
:)
Post Reply