You are here: Re: how do i avoid typing the keyword "$this->" every time inside a class « PHP Programming Language « IT news, forums, messages
Re: how do i avoid typing the keyword "$this->" every time inside a class

Posted by Joseph S. on 08/11/06 06:15

Hi all,
first, thanks for all the replies.
(1)
>Unfortunately, you can't.
>$this->foo is a class variable.
>$foo is a local variable.
>==================
>Remove the "x" from my email address
>Jerry Stuckle
ok. Thinking of it, IMHO, this is a big excuse we (PHP community) give
to show minus points about PHP - Refer to
http://groups.google.co.in/group/comp.lang.php/browse_thread/thread/b9260666509a9c36/5369d366737bb9d3?lnk=st&q=&rnum=1#5369d366737bb9d3

(2)
william.clarke wrote:
> Sorry should have posted links:
> PhpED: http://www.nusphere.com/products/phped.htm
> Zend: http://www.zend.com/products/zend_studio/feature_list
> Obviously neither of these is free, but you could try Eclipse with a Php add-on.
I am using PHPEclipse for a long time now, (but wrote all my code in
procedural style, not touching OOP) - but it pops up huge autocomplete
list, and somehow, no "$this" - i'll have to look at the configuration
- (thanks anyway)

(3)
My own personal opinion is as follows:
if not for the "$" and the "->", PHP would have _eaten_ up a lot more
of the market share - that it is already doing so is not surprising. It
is simply the easiest language to work in. Lots of flexibility,
plethora of well designed functions and a very pratical approach,
rather than the purist apporach of Java/.Net designers which result in
more code being required to be written to accomplish simple tasks -
most obvious examples are file() and file_get/put_contents().

A Google summer of code project promises something interesting PHP
Preprocessor Macros:
http://code.google.com/soc/php/appinfo.html?csaid=3752FBA8CFFCD528

Finally, as a practical workaround, maybe a simple global function
could be written, say, function v($arg){
return (eval("\$this->".variable_name($arg));
}
I am looking for the function variable_name(). Looked around a bit, but
could not find one.

Or better still, one could write a slightly complex parsing script, say
code-cleaner.php, send original code to that script
(PHP in CLI mode:)
php -n code-cleaner.php myscript.php

which will do all the substitution and overwrite myscript.php with
"$this->" code.
I'll try this and post it if it succeeds.

One of the important things here is that PHP allows two $foo variables
in one class:
$foo = 5;
class A{
public $foo = 6;
public function showfoo(){
echo $foo; // 6
echo $this->foo; // 5
}
}
and that is why $this is required. I believe (please correct if wrong)
other languages give errors like "$foo is ambiguous". I dont know as
much of compiler internals as to know why $this-> is required
_everywhere_.
This should have gone into PHP5, really, during the huge OOP rewrite.

Meanwhile,
people could have a look at
http://groups.google.com/group/comp.lang.php/browse_thread/thread/deea8ea577378c21/d0af228658807cee?lnk=gst&q=how+to+get+name+of+variable+from+reference&rnum=3#d0af228658807cee
which shows how to get the name of a variable.

Regards,
JS.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация