Class/function scope general question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm doing some re-writing of a huge class I've got (don't think OOP cause it's really not, just the usual class full of functions). What I'm doing is moving the functions out of the class and into separate files so the (I'm hoping) memory footprint will be smaller.

The basic setup I started with is:

class foo {
	
	function doSomething()
	{
		switch($var) {
		case '1':
			$this->bar();
			break;
		case '2':
			$this->baz();
			break;
		}
	}

	function bar(){ // do something useful }

	function baz(){ // do something else useful }

	[...]
}

I've moved bar() and baz() into their own file and am now including those files from the original functions as so:

class foo {
	
	function doSomething()
	{
		switch($var) {
		case '1':
			$this->bar();
			break;
		case '2':
			$this->baz();
			break;
		}
	}

	function bar(){ include_once 'bar.php'; newBar(); }

	function baz(){ include_once 'baz.php'; newBaz(); }

	[...]
}

where newBar() and newBaz() are just the functions copied from the original class like

bar.php
<?php
function newBar(){ // do something useful }

Now the interesting bit I don't quite comprehend...

var_dump($this); from inside newBar() returns null.

Sort of unexpected behavior to me, but only slightly. Shouldn't the newBar() function pick up the scope from foo::bar() since, as I understand includes, it's the same as writing the code in the file where the include statement is at?

Outside of newBar() in bar.php var_dump($this) gives me the foo class.

Is it because there is basically a function within a function at this point and somehow the scope of $this is being lost? I would have thought that it would carry down, but obviously I'm wrong.

Not looking for a fix really, I'm passing in a reference to $this to newBar() so it's all cool there, just looking for an explanation to have for future reference.

PHP4.4.2 btw... if that makes any difference.

Thanks!
Ed

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux