473,513 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3061
"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
1774
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 CLASSID="CLSID:D3BEF9B9-F7B5-4C24-B247-32257F37FDB6" CODEBASE="COM.CAB#version=1,0,0,1"></OBJECT></HTML> As per my understanding if client access this page for the...
4
2457
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
1549
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 some code that is GUARANTEED to run EVERY time the code exits? I don't mind if it runs during garbage collection or whatever. I just need it to run...
6
1437
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
2809
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 determine if the website is local or remote and if it is local if it is the test site or "live" site then it sets the 4 variables. I am trying to...
1
1150
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 the event for listbox also called.. can anybody suggest how to avoid this event on every page load.... Irfan
3
2668
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 be a ENDNOW box stating WMSIdle unless i click on EndNow My Pc wont be Turned OFF... please Provide me solution for both of this if possible...
1
1406
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 No Investment. No Joining Fees. 100% Free to Join. Home based Part time Online Jobs.
26
2314
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
1953
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 that item data will getting from WCF restful service.But every time total page is loading.According to my client guide we are not using AJAX.So ...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5704
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.