Page 1 of 1

What is this syntax?

Posted: Thu Sep 24, 2009 11:26 am
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

Re: What is this syntax?

Posted: Thu Sep 24, 2009 12:06 pm
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

Re: What is this syntax?

Posted: Thu Sep 24, 2009 12:12 pm
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 [.]?

Re: What is this syntax?

Posted: Thu Sep 24, 2009 12:16 pm
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.

Re: What is this syntax?

Posted: Thu Sep 24, 2009 1:34 pm
by pchika
Got it, makes perfect sense. Thanks to all!!

Re: What is this syntax?

Posted: Thu Sep 24, 2009 5:07 pm
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;
?>
:)