Click to See Complete Forum and Search --> : Calling a function for the page
bharanidharanit
Feb 2nd, 2010, 01:32 AM
Hi,
My application contains 2 php pages, main.php and functions.php. the functions php file has many php functions in it.
I am including the functions.php file in main.php using require_once(), but after including the file i only want certain functions to be called from functions.php file.
Any ideas, thankyou.
I_Love_My_Vans
Feb 2nd, 2010, 03:19 AM
So you're saying you have function x, y and z in your functions.php page, but you only want to call x and y? Surely this can be achieved... by not calling them?
If not please explain your requirements in more detail.
kows
Feb 2nd, 2010, 08:29 AM
yeah, what ILMV said. a function is just a piece of code that sits there; it doesn't actually do anything unless you call it. functions are made to be able to re-use code to do common actions. there wouldn't be much point in functions if they ran whenever they were parsed.
NightWalker: ... I don't know what you're trying to say here, but he didn't say anything about having a class (and from my limited experience, I'm not sure he even knows what a class is). you can't just make up a class and call global functions from it.
bharanidharanit
Feb 2nd, 2010, 10:03 AM
thankyou all, ya i am very very beginner in class, anyway i tried some and this works for me. is this correct?
functions.php
class sample
{
function test1()
{
echo "test1";
}
function test2()
{
echo "test2";
}
}
main.php
<?PHP
require_once('functions.php');
$req = new sample;
$req -> test1();
?>
kows
Feb 2nd, 2010, 10:06 AM
that is the correct way to create an object and call a method from it, yes.
bharanidharanit
Feb 2nd, 2010, 10:09 AM
ya thankyou, is there anything like destructing the used class here?
kows
Feb 2nd, 2010, 02:15 PM
you can destroy an object in PHP, but it's not really needed. for anything I can think of that you might be doing, I can't see why you'd need to bother.
bharanidharanit
Feb 2nd, 2010, 04:26 PM
ya thankyou this will be ok now
Nightwalker83
Feb 2nd, 2010, 06:08 PM
NightWalker: ... I don't know what you're trying to say here, but he didn't say anything about having a class (and from my limited experience, I'm not sure he even knows what a class is). you can't just make up a class and call global functions from it.
Ah ok! That was just part of a working code I had on hand and reading his post
following yours he understood what I was trying to say.
bharanidharanit
Feb 3rd, 2010, 05:53 AM
Hi i cant able to call another function within the class.
class sample
{
function test1()
{
echo "test1";
test2();
}
function test2()
{
echo "test2";
}
}
This doesnot works for me, it displays error as function not defined.
kows
Feb 3rd, 2010, 11:06 AM
that's because you're using a class. classes are different; you can't access something in a class by just calling a function's name. this will make PHP look for that function outside of the class, in the global scope. this is why you have the $this keyword to specify that you're trying to access methods within the current class. you should really read about objects in PHP (http://php.net/manual/en/language.oop5.php) before you try to mess around with them too much.
visualAd
Feb 4th, 2010, 07:00 AM
Ah ok! That was just part of a working code I had on hand and reading his post
following yours he understood what I was trying to say.
You should delete your original post, because all it is doing is causing confusion.
bharanidharanit; you are best off removing the functions from the class and do what you were doing before. If you do not intend to call certain functions then I suggest you separate these into another file too; perhaps group them according to what they do and put each group into a file of its own.
As kows says, it is best not to use objects (classes) unless you know how make them work to your advantage. Otherwise you risk further complicating something that is really quite simple. Please ignore the original reply to this thread.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.