Connecting Tech Pros Worldwide Forums | Help | Site Map

what's this? '->'

gmac63@charter.net
Guest
 
Posts: n/a
#1: Jul 17 '05
All,

I know that '->' is used in object oriented PHP, but I saw this in the
Moodle code (www.moodle.org)

$CFG->dbtype = 'mysql'; // mysql or postgres7 (for now)
$CFG->dbhost = 'localhost'; // eg localhost or db.isp.com
$CFG->dbname = 'moodle'; // database name, eg moodle
$CFG->dbuser = 'root'; // your database username
$CFG->dbpass = 'mypassword'; // your database password
$CFG->prefix = 'mdl_'; // Prefix to use for all table names

You can "echo $CFG->dbtype;" and get "mysql"

This isn't calling functions from an object, rather seems to be
applying properties to a new "object"??? There is no $something = new
ClassName, so This is what I figure.

Its been a while since I cooded PHP and have never seen this. This
seems like a realy good way to apply a number of atttribs to a
"variable" without having to create an array -- unless that is a
shorthand way of creating an array...

Thanks

-Wes Yates


Mark
Guest
 
Posts: n/a
#2: Jul 17 '05

re: what's this? '->'


gmac63@charter.net wrote:
[color=blue]
> All,
>
> I know that '->' is used in object oriented PHP, but I saw this in the
> Moodle code (www.moodle.org)
>
> $CFG->dbtype = 'mysql'; // mysql or postgres7 (for now)
> $CFG->dbhost = 'localhost'; // eg localhost or db.isp.com
> $CFG->dbname = 'moodle'; // database name, eg moodle
> $CFG->dbuser = 'root'; // your database username
> $CFG->dbpass = 'mypassword'; // your database password
> $CFG->prefix = 'mdl_'; // Prefix to use for all table names
>
> You can "echo $CFG->dbtype;" and get "mysql"
>
> This isn't calling functions from an object, rather seems to be
> applying properties to a new "object"??? There is no $something = new
> ClassName, so This is what I figure.
>
> Its been a while since I cooded PHP and have never seen this. This
> seems like a realy good way to apply a number of atttribs to a
> "variable" without having to create an array -- unless that is a
> shorthand way of creating an array...
>[/color]

you need to read this:

http://www.php.net/manual/en/language.oop5.basic.php


they're member variables, which you can also define and get/set on an
object.


mark.



--
I am not an ANGRY man. Remove the rage from my email to reply.
Wayne
Guest
 
Posts: n/a
#3: Jul 17 '05

re: what's this? '->'


On 11 Feb 2005 09:15:55 -0800, gmac63@charter.net wrote:

[color=blue]
>You can "echo $CFG->dbtype;" and get "mysql"
>
>This isn't calling functions from an object, rather seems to be
>applying properties to a new "object"??? There is no $something = new
>ClassName, so This is what I figure.[/color]

If you use a variable as an object PHP automatically creates an object
(I believe this behavior is depreciated in PHP5). There's an implicit
"$something = new stdClass;" if you use a variable as an object.
[color=blue]
>This
>seems like a realy good way to apply a number of atttribs to a
>"variable" without having to create an array -- unless that is a
>shorthand way of creating an array...[/color]

Yeah, I use this all the time. It works with any class...

class SomeClass()
{
}

$test = new SomeClass();
$test->newVar = 'hello';

echo $test->newVar; // prints 'hello';


gmac63@charter.net
Guest
 
Posts: n/a
#4: Jul 17 '05

re: what's this? '->'


WOW! Now that _is_ cool. I figures as much that it creates an object.
The "->" is what PHP uses in oop. Saaaa-wweeeeeet!

Thanks all!

-Wes

Wayne wrote:[color=blue]
> On 11 Feb 2005 09:15:55 -0800, gmac63@charter.net wrote:
>
>[color=green]
> >You can "echo $CFG->dbtype;" and get "mysql"
> >
> >This isn't calling functions from an object, rather seems to be
> >applying properties to a new "object"??? There is no $something =[/color][/color]
new[color=blue][color=green]
> >ClassName, so This is what I figure.[/color]
>
> If you use a variable as an object PHP automatically creates an[/color]
object[color=blue]
> (I believe this behavior is depreciated in PHP5). There's an[/color]
implicit[color=blue]
> "$something = new stdClass;" if you use a variable as an object.
>[color=green]
> >This
> >seems like a realy good way to apply a number of atttribs to a
> >"variable" without having to create an array -- unless that is a
> >shorthand way of creating an array...[/color]
>
> Yeah, I use this all the time. It works with any class...
>
> class SomeClass()
> {
> }
>
> $test = new SomeClass();
> $test->newVar = 'hello';
>
> echo $test->newVar; // prints 'hello';[/color]

Closed Thread