|
-
Feb 2nd, 2010, 02:32 AM
#1
Thread Starter
Fanatic Member
Calling a function for the page
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.
-
Feb 2nd, 2010, 04:19 AM
#2
Re: Calling a function for the page
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.
-
Feb 2nd, 2010, 09:29 AM
#3
Re: Calling a function for the page
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.
-
Feb 2nd, 2010, 11:03 AM
#4
Thread Starter
Fanatic Member
Re: Calling a function for the page
thankyou all, ya i am very very beginner in class, anyway i tried some and this works for me. is this correct?
functions.php
Code:
class sample
{
function test1()
{
echo "test1";
}
function test2()
{
echo "test2";
}
}
main.php
Code:
<?PHP
require_once('functions.php');
$req = new sample;
$req -> test1();
?>
Last edited by bharanidharanit; Feb 2nd, 2010 at 11:03 AM.
Reason: wrong one
-
Feb 2nd, 2010, 11:06 AM
#5
Re: Calling a function for the page
that is the correct way to create an object and call a method from it, yes.
Last edited by kows; Feb 2nd, 2010 at 11:10 AM.
-
Feb 2nd, 2010, 11:09 AM
#6
Thread Starter
Fanatic Member
Re: Calling a function for the page
ya thankyou, is there anything like destructing the used class here?
-
Feb 2nd, 2010, 03:15 PM
#7
Re: Calling a function for the page
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.
-
Feb 2nd, 2010, 05:26 PM
#8
Thread Starter
Fanatic Member
Re: Calling a function for the page
ya thankyou this will be ok now
-
Feb 2nd, 2010, 07:08 PM
#9
Re: Calling a function for the page
 Originally Posted by kows
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.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Feb 3rd, 2010, 06:53 AM
#10
Thread Starter
Fanatic Member
Re: Calling a function for the page
Hi i cant able to call another function within the class.
Code:
class sample
{
function test1()
{
echo "test1";
test2();
}
function test2()
{
echo "test2";
}
}
This doesnot works for me, it displays error as function not defined.
-
Feb 3rd, 2010, 12:06 PM
#11
Re: Calling a function for the page
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 before you try to mess around with them too much.
-
Feb 4th, 2010, 08:00 AM
#12
Re: Calling a function for the page
 Originally Posted by Nightwalker83
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|