Jump to content

Quick Question About Function Scope


glenelkins

Recommended Posts

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

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!!

 

 

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.

 

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!??

 

 

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.