glenelkins Posted February 13, 2009 Share Posted February 13, 2009 Hi Lets say i have a function and class like follows; Basically i need to know if i can access the test() function within the class, there must be a way to do this? With a variable id just put global $variable; inside the class, i wonder if global function(); does the same? function test() { } class Test { function Test() { test(); // How can i make this function work? } } Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/ Share on other sites More sharing options...
corbin Posted February 13, 2009 Share Posted February 13, 2009 The Test() method of a Test class would be the constructor.... But anyway, you could call it statically with Test::Test(); Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761387 Share on other sites More sharing options...
allworknoplay Posted February 13, 2009 Share Posted February 13, 2009 Hi Lets say i have a function and class like follows; Basically i need to know if i can access the test() function within the class, there must be a way to do this? With a variable id just put global $variable; inside the class, i wonder if global function(); does the same? function test() { } class Test { function Test() { test(); // How can i make this function work? } } Although I am new to OOP. I think the scope in which you are creating your class is wrong. The class is the biggest scope you can have when creating a class, so you can't have any other functions outside of that class... If I am wrong, please someone correct me as well!! Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761393 Share on other sites More sharing options...
Mark Baker Posted February 13, 2009 Share Posted February 13, 2009 The test() function should be accessible from inside or outside of your class, without any further need for scoping, in exactly the same way as PHP's built-in functions like strlen() are accessible from inside or outside of a class. The Test() method within the Test class is different. As the constructor method, it's called automatically when you instantiate an object of type Test, or can be called from outside of the class using $testObject->Test() assuming that your instantiated Test object is $testObject... or from within the class as $this->Test(), probably not advisable because it is the constructor. Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761402 Share on other sites More sharing options...
allworknoplay Posted February 13, 2009 Share Posted February 13, 2009 The test() function should be accessible from inside or outside of your class, without any further need for scoping, in exactly the same way as PHP's built-in functions like strlen() are accessible from inside or outside of a class. Ok, I can see how this would work if the class and test() function were written in the same file. But if you were to make the above example in a separate class file, and then try to instantiate the class, there's no way you should be able to access the test() function if it was in the class file but outside of the class scope!?? Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761408 Share on other sites More sharing options...
Mark Baker Posted February 13, 2009 Share Posted February 13, 2009 Ok, I can see how this would work if the class and test() function were written in the same file. But if you were to make the above example in a separate class file, and then try to instantiate the class, there's no way you should be able to access the test() function if it was in the class file but outside of the class scope!??If test() was in an included file, it should still work. Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761412 Share on other sites More sharing options...
corbin Posted February 13, 2009 Share Posted February 13, 2009 Oh wow.... I misunderstood your question. I thought you were trying to execute Test::Test(), not test(). Ignore my earlier post ;p. Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761413 Share on other sites More sharing options...
glenelkins Posted February 13, 2009 Author Share Posted February 13, 2009 Mark..cheers thats the answer i wanted. I already know about the constructor lol it was the outside test function i was talking about Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761619 Share on other sites More sharing options...
glenelkins Posted February 13, 2009 Author Share Posted February 13, 2009 no worries corbin the reason for my question is im working on a custom CMS. None of the cms systems i have used give me enough imediate flexibility as i develop allot of content managed sites...textpattern is the closest to me liking one of the current ones. im writing a function like $obj = copy_instance() which will return a copy of the main class object Link to comment https://forums.phpfreaks.com/topic/145091-quick-question-about-function-scope/#findComment-761627 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.