473,382 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

how do i avoid typing the keyword "$this->" every time inside a class

Hi all,
how do I avoid typing the keyword "$this->" every time I need to
reference a member of a class inside the class?

(coming from a world of cozy auto-complete enabled Java / .Net IDEs I
find it a bit annoying)

TIA,
JS

Aug 9 '06 #1
11 3055
"Joseph S." <js****@rediffmail.comwrote:
how do I avoid typing the keyword "$this->" every time I need to
reference a member of a class inside the class?
Set up a function key macro in your editor?

miguel
--
Photos from 40 countries on 5 continents: http://travel.u.nu
Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
Airports of the world: http://airport.u.nu
Aug 9 '06 #2
Joseph S. wrote:
Hi all,
how do I avoid typing the keyword "$this->" every time I need to
reference a member of a class inside the class?

(coming from a world of cozy auto-complete enabled Java / .Net IDEs I
find it a bit annoying)

TIA,
JS
For what it's worth, I use Zend studio, and with it's autocomplete,
typing $t is usually enough to get $this as the first entry in most
cases.

I agree, though, that it can get a bit tedious after a while in any
decent sized class.

Aug 9 '06 #3
Joseph S. wrote:
Hi all,
how do I avoid typing the keyword "$this->" every time I need to
reference a member of a class inside the class?

(coming from a world of cozy auto-complete enabled Java / .Net IDEs I
find it a bit annoying)
back when the world was young, in foxpro we would do this:

WITH THIS
.property1 = 'value'
.property2 = 'value'

ENDWITH

AFAIK this language element does not exist in PHP.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Aug 9 '06 #4
Joseph S. wrote:
Hi all,
how do I avoid typing the keyword "$this->" every time I need to
reference a member of a class inside the class?

(coming from a world of cozy auto-complete enabled Java / .Net IDEs I
find it a bit annoying)

TIA,
JS
Unfortunately, you can't.

$this->foo is a class variable.

$foo is a local variable.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 10 '06 #5
Joseph S. wrote:
Hi all,
how do I avoid typing the keyword "$this->" every time I need to
reference a member of a class inside the class?

(coming from a world of cozy auto-complete enabled Java / .Net IDEs I
find it a bit annoying)

TIA,
JS
I usually copy paste it, or use those column select (aka block select)
feature of good editors for multiple lines. Edit plus comes to mind.
Aug 10 '06 #6

s a n j a y wrote:
Joseph S. wrote:
Hi all,
how do I avoid typing the keyword "$this->" every time I need to
reference a member of a class inside the class?

(coming from a world of cozy auto-complete enabled Java / .Net IDEs I
find it a bit annoying)

TIA,
JS

I usually copy paste it, or use those column select (aka block select)
feature of good editors for multiple lines. Edit plus comes to mind.
I usually just type it, it is just a few extra characters. It doesn't
take up that much time, unless you type very slowly...

Aug 10 '06 #7
Why not get an editor with autocomplete...have a look at PHPed, Zend
Studio and lots of the other IDEs out there.
When I started doing Java I did it in the "cosy" Notepad IDE with a
command line JDK compiler, so strangely autocomplete didn't work there
either. If your tools aren't doing the job, find better tools instead
of looking for shortcuts in the language ;)

Aug 10 '06 #8
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.

Aug 10 '06 #9
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/com...69d366737bb9d3

(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/appin...52FBA8CFFCD528

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....af228658807cee
which shows how to get the name of a variable.

Regards,
JS.

Aug 11 '06 #10

Joseph S. wrote:
(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)
The PHP plugin for eclipse is mediocre at best. I find I fight it most
of the time:

1) New lines within quoted strings: it will auto end the line and make
it concatenate, but PHP will allow returns within the line, VERY
annoying when you're writing quoted HTML attributes within a HEREDOC
block.
2) Very poor autocomplete compared to other programs. $this-pops up
a huge list of potential matches. using built in functions will only
display the parameter list until you start the parentheses, in which
case it starts suggesting variables, which usually start with the super
globals. VERY frustrating.
3) Aggresive auto-formatting:
* Auto-inserts closing } after a { even if it is within a closed block
that it, itself, closed, creating a syntax error.
* The opening of a block with { will begin to suggest variables, and
pressing enter will auto-insert the first variable, which is usually
$_GET.
* Auto-complete of variables is iffy, too often has i tried to use
auto-complete, typed the first few characters, tab'd, and it only left
those characters instead of completing the variable name, even if it
was the only variable available.
4) Poor scope detection: it frequently suggests global variables that
aren't declared as global within the scope, thus would be inaccessible.
5) Poor auto-complete of auto-globals. Dreamweaver will display an
autocomplete for variables within $_SERVER, which is very handy. PHP
Eclipse won't.
6) No concept of current type of variable (If you've use vs.php, the
php plugin for the visual studio, you know what i mean - it can
somewhat accurately guess the type of a variable).
7) Poor help tips, see (2). You can document all your methods with
@param, @return, but you'll never see these comments from auto-complete
unless you manually hover your mouse over the method...defeating the
point of a context help as you type.
8) I could go on, but i think this is enough.

If you can get a hold of the visual studio IDE, try the vs.php plugin.
I find it vastly superior to eclipse's

(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().
Why? I don't see how $ and -has much effect on the market share.
A Google summer of code project promises something interesting PHP
Preprocessor Macros:
http://code.google.com/soc/php/appin...52FBA8CFFCD528

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_.
In PHP you have to qualify class/instance variables with $this or
classname::, if it isn't, its considered a local variable, more or less
(exceptions being 'global $var' and super globals)
In most languages, there's a heirarchy of scope. In general, most
specific overrides least specific:

If we were to consider some sort of php/java/c++ hybrid:
$foo = '3';
class bar {
public $foo = '2';
function baz($foo='1';) {
$foo // local variable, 1
$this.$foo // instance, 2
$GLOBALS['foo'] // global variable, 3
}
}

in java, you can do:

class bar {
public name;
void myfunc(String name) {
name = "John"; // refers to local name
this.name = "Mary"; // refers to instance name
}
}

Most languages have that sort of heirarchy, anyways.

And in response to original poster: Just use $this->, its not that much
of a hassle.
If you dont' like typing $this-before every var, just do $myvar = &
$this->myvar; at the start of the method.

Aug 11 '06 #11

Richard Levasseur wrote:
>
Why? I don't see how $ and -has much effect on the market share.
I don't think it really does, either, but I just have to question the
wisdom of choosing the "->" over something like, say ".", which as you
said later in your post, some other languages have.

A "." is quick, simple, one keystroke, and my finger is right near that
key anyway.
A "->", on the other hand, requires me to move a finger to the numeric
row, then hold shift and hit another key (3 keystrokes vs 1).

As you mentioned, many other languages require scoping of class
variables vs local variables via a 'this', I think PHP just compounds
the issue with its choice of "->".

Yes, it's nice that it looks like an arrow and all, but can we leave
the ASCII art to the kiddies and instead make a language that favors
speed in writeability?

Aug 11 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chad | last post by:
I have COM component embedded on html page. It has version 1.0.0.1 (by default). I have converted it in cab file. Code of html is something like: <HTML><HEAD></HEAD><BODY><OBJECT...
4
by: murat oguzalp | last post by:
i want to call base and this constructor at the same time. is is possible? i mean: B(int a, int b, int c):base(a):this(b) { // do something with c } at java i used to do that:
8
by: Scott M | last post by:
I have a procedure in my Dispose method that records my application was closed in a database. However, when I kill a process or an unhandled exception occurs this does not seem to run. Is there...
6
by: Moses | last post by:
Hi All, How to avoid the execution of a function at the time of defining. Here i am giving the details. I am creating the following div container through DOM.
9
by: Justin Voelker | last post by:
I have a configuration file that contains the host, username, password, and database name required for any database connections. The config file runs through a few if/then/else statements to...
1
by: irfanms | last post by:
Hi; Can anyone help me for this... I've dyanamically created controls in my webpage.It contains calender & listbox and combo also. It has button too. when button is pressed after page load event...
3
cooldilip
by: cooldilip | last post by:
This is My problem Every time i Start My PC... There will be a POP Up Saying The Search Keyword... RVTHOST.EXE doesnot exist... can you help to get rid of this... and During ShutDown there will...
1
by: yuva | last post by:
"Thank you for this amazing program. Within a weekend, my team increased with 50 new people joining. Every time I logged on there was a new affiliate. Absolutely amazing. " S. Horvath SOUTH AFRICA...
26
by: Ravindra.B | last post by:
I have declared a global variable which is array of pointers and allocated memory for each array variable by using malloc. Some thing similar to below... static char *arr; main() { int i;
1
by: phanimadhav | last post by:
Hello Experts, I am new one of this WCF.Actually my client requirement is avoid the page load every timedropdown_selected index changes.If i select the item in dropdown based on...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.