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!
General Semantics of PHP
Moderator: General Moderators
Re: General Semantics of PHP
I see a problem here.HrPeanut wrote:I am currently writing a paper about PHP... I'm still a little new to PHP
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.
Re: General Semantics of PHP
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:I see a problem here.HrPeanut wrote:I am currently writing a paper about PHP... I'm still a little new to PHP
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.
Thanks for some explanation, the link you provided really helped.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").
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__
Re: General Semantics of PHP
I would definitely start with reading the manual. http://www.php.net/manual/en/
Re: General Semantics of PHP
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
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).
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);
}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).
Re: General Semantics of PHP
thanks tasairis,
your replies helped me out a lot. I was able to finish this section!

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