General Semantics of PHP

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
HrPeanut
Forum Newbie
Posts: 3
Joined: Sat Dec 06, 2008 5:52 pm

General Semantics of PHP

Post by HrPeanut »

Hello!

First of all, sorry if I posted this in the wrong section.. I'm rather new here. :p

I am currently writing a paper about PHP. This includes Syntax, Semantics, and numerous other things like Support for OO and Concurrency. I'm still a little new to PHP as I have only done some things with databases in the past, and I am struggling a little bit with some Semantics. I have, however, Isolated somethings I wish to cover in this section.

I would like to understand the purpose and usage of the '$' symbol in front of every variable. - Is this used for more than just variables?

I would like to understand the purpose and usage of the '->'. In C++, this is used to access elements in a reference. How is it used in PHP?

I would also like an explanation of the '__'. I'm assuming it's for reserved PHP keywords, but I cannot be sure yet.

If anyone could help me out here, it would be greatly appreciated. I've ran the google machine a couple times but no such luck.

Thanks in advance!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: General Semantics of PHP

Post by requinix »

HrPeanut wrote:I am currently writing a paper about PHP... I'm still a little new to PHP
I see a problem here.

All your questions can be answered if you learn PHP. You're writing a paper about it for some reason, it would make sense if you actually knew what you were talking about and weren't just reciting what others told you.

$ marks variables and only variables. -> is used to access stuff belonging to an instantiated class. __ is a convention, a prefix typically used to mark magic methods (methods that have a special meaning beyond just "yet another method").
PHP is the way it is because somebody decided to do it that way.
HrPeanut
Forum Newbie
Posts: 3
Joined: Sat Dec 06, 2008 5:52 pm

Re: General Semantics of PHP

Post by HrPeanut »

tasairis wrote:
HrPeanut wrote:I am currently writing a paper about PHP... I'm still a little new to PHP
I see a problem here.

All your questions can be answered if you learn PHP. You're writing a paper about it for some reason, it would make sense if you actually knew what you were talking about and weren't just reciting what others told you.
Well, maybe a better phrase would be "I'm not an expert at PhP." I have used PHP in the past developing some Websites and Database API's, so I'm not completely new. The purpose of this assignment is to develop your learning ability of an unfamiliar language, which is what this current class is all about.
tasairis wrote: $ marks variables and only variables. -> is used to access stuff belonging to an instantiated class. __ is a convention, a prefix typically used to mark magic methods (methods that have a special meaning beyond just "yet another method").
Thanks for some explanation, the link you provided really helped.
About magic methods though - they all seem to be recognized by the language. Maybe not as Reserved, but as Pre-existing. Is this true? I have found some other things such as __FILE__
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: General Semantics of PHP

Post by califdon »

I would definitely start with reading the manual. http://www.php.net/manual/en/
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: General Semantics of PHP

Post by requinix »

Ah, forgot about the constants.

Most (all but one, I think) magic methods are for classes. For example, if you define __get for a class, when trying to access a variable that's unavailable (declared private/protected or doesn't exist) this method will get called, allowing you to do something special. There's a __set that works for setting variables.
If you don't define those then you don't get the functionality associated with them. You could pretend that they already exist with code like

Code: Select all

public function stdClass::__get($variable) {
    trigger_error("Error message about how \$$variable does not exist", E_WARNING);
}
and can be overridden.

There's also the __autoload function.

And of course, the few magic constants: things that behave like constants (no scope, cannot be altered) but whose values can vary. If you have the minimum PHP version, each of those constants exist when used in the right scope (can't use __CLASS__ outside of a class, etc).
HrPeanut
Forum Newbie
Posts: 3
Joined: Sat Dec 06, 2008 5:52 pm

Re: General Semantics of PHP

Post by HrPeanut »

thanks tasairis,
your replies helped me out a lot. I was able to finish this section!

:D
Post Reply