Re: [PHP] Class/function scope general question
On May 15, 2006, at 7:53 PM, Richard Lynch wrote: You can't do that. Yeah I can, sorta. Well not really as I'm having to pass a reference to the class object around. But that works. The whold class has to be in a single contiguous file. Last I checked. To be able to use $this- in the functions yeah, although it was available in the file, outside of a function but that could get *real* dangerous. On the plus side, it's incredibly unlikely that having the functions in separate files was a Good Idea... Well... I know it's not a *good* idea, it's just an intermediate step that's helping me see the class pieces a little better so I *can* figure out the best way to go. I've got a monster that has grown out of control over time that really needs to be tamed. Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class/function scope general question
You can't do that. The whold class has to be in a single contiguous file. Last I checked. On the plus side, it's incredibly unlikely that having the functions in separate files was a Good Idea... On Fri, May 12, 2006 12:34 pm, Edward Vermillion wrote: 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 -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Class/function scope general question
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
Re: [PHP] Class/function scope general question
2006/5/12, Edward Vermillion [EMAIL PROTECTED]: 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 When you call a function in the global scope from inside a member function you're leaving the object scope, that's why this is null in the global function
Re: [PHP] Class/function scope general question
On May 12, 2006, at 1:09 PM, Martin Alterisio wrote: [snip] When you call a function in the global scope from inside a member function you're leaving the object scope, that's why this is null in the global function But my thought is that since the include was from inside the member function that the included function would be in the scope of the member function, and not global. (?) So if I have: class foo { function bar() { function baz(){} } } Would baz() be out of class scope here? Or are includes automatically global no matter where they are called? Or is it the convoluted manner in which I'm getting to baz() that's causing this? Thanks! Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class/function scope general question
Edward Vermillion wrote: But my thought is that since the include was from inside the member function that the included function would be in the scope of the member function, and not global. (?) So if I have: class foo { function bar() { function baz(){} } } Would baz() be out of class scope here? Or are includes automatically global no matter where they are called? Or is it the convoluted manner in which I'm getting to baz() that's causing this? Scope is your issue here. Functions and classes defined in included files are registered at the global level. Therefore within your include file you do not have a $this because your function is actually defined outside the class. The example you give above I'm not so sure about. I believe baz() wouldn't have a $this variable, but stick some code in it to check. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Class/function scope general question
2006/5/12, Edward Vermillion [EMAIL PROTECTED]: On May 12, 2006, at 1:09 PM, Martin Alterisio wrote: class foo { function bar() { function baz(){} } } baz() will be a global function there. There are other ways to add member functions at runtime but I haven't had the chance to properly used to them to say they are safe or not.
Re: [PHP] Class/function scope general question
On May 12, 2006, at 1:55 PM, Stut wrote: Edward Vermillion wrote: But my thought is that since the include was from inside the member function that the included function would be in the scope of the member function, and not global. (?) So if I have: class foo { function bar() { function baz(){} } } Would baz() be out of class scope here? Or are includes automatically global no matter where they are called? Or is it the convoluted manner in which I'm getting to baz() that's causing this? Scope is your issue here. Functions and classes defined in included files are registered at the global level. Therefore within your include file you do not have a $this because your function is actually defined outside the class. Thanks! I didn't know that, and it's a good piece of info to have. The example you give above I'm not so sure about. I believe baz() wouldn't have a $this variable, but stick some code in it to check. Hopefully I'll never have to do anything that ridiculous (the example), at least it seems so now. Although I am doing sort of the same thing. Hopefully this is just an intermediate step (including functions with an include() in a class method) to the proper solution for my problem (it just seems wrong but I need something that will work for now). Like I said, it wasn't a big surprise that I had to pass in a reference to the foo class, but it did make me wonder why. Thanks for all the help! Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php