headbanner

Menu:

nigel-1

Quick Notes:

April 20th, 2008:
Ruby is all syntactic sugar once you get into it. Its not a bad thing, but one should remember that. Rails does too much at run time, and I'm missing JavaScript. Need to finish my XUL based XDebug client for PHP.

Read more...

About Nigel

Nigel is a consultant at ThoughtWorks, seeking a code free nirvana. Unfortunately he's missed the second left.

Previous Posts

- Implementing a Singleton Design pattern in PHP

- Unicode Rendering in Firefox is broken.

- Questions for the Campers at BarCampMumbai2

- GTAC 2007, Selenium, and the UI as objects in test...

- Installing Xdebug for PHP on Centos 4 (server)

- Learning Javascript the right way.

Archives

- August 2007
- October 2007
- January 2008
- February 2008
- March 2008

My choice of links
worth visiting

Check out

- BookEazy
- Intermission
- Sukshma
- CodeWord
- TechCrunch

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]

Hi. You've reached Nigel.in

This site is still in its very early stages, but what you have landed on, possibly inadvertently, is the website of a certain curious character a.k.a Nigel Fernandes

The aim of this page was to serve as a landing point for those of you who want to know a little more about me. The links on the right and left will take you deeper my world, or possibly on to different exciting things.

Its a big web out there and this is just one more street for you to saunter down. I hope you like it.

Close this.

My Photo
Name:
Location: Panjim, Goa, India

Crazy, dancing, programming, Goan.. I'm a computer geek and proud to be one. I program in Java, Ruby and .Net, PHP, Javascript. A lot of my recent work has been about CSS and UI design practices for large scale websites and Agile teams. I still while away hours dreaming up a web startup.

 

Interesting PHP function scoping caveat

Friday, February 29, 2008

While playing around a personal PHP project the other day I stumbled across some rather unsual PHP scope scenario. I was in the process of migrating some instance methods to static methods when I discovered the following:

Lets look at the some PHP code first:
class BarClass {

public function readData () {
echo $this->data;
}

public static function readData2 () {
echo $this->data;
}
}

class FooClass {

public $data;

public function __construct ($parameter) {
$this->data = $parameter;
}

function testScope () {
BarClass::readData();
}

function testScopeFail() {
BarClass::readData2();
}
}

$myObj= new FooClass('Spooky Scope');
$myObj->testScope(); // Will print 'Spooky Scope'
$myObj->testScopeFail(); // Will throw an error
What is so intriguing in this little snippet of code is that I'm accidentally calling the BarClass method readData() statically, yet it has the $this instance variable defined within it ...and set to the calling scope!

Amazing. I wonder what kind of interesting things I could conjure up with this.

I included the other method readData2() as part of this post because a static function if properly defined will throw an error. ;-)

Labels: ,


Comments:
idiot
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home