Results 1 to 12 of 12

Thread: Calling a function for the page

  1. #1

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    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.

  2. #2
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    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.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  4. #4

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    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

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  6. #6

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Calling a function for the page

    ya thankyou, is there anything like destructing the used class here?

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  8. #8

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: Calling a function for the page

    ya thankyou this will be ok now

  9. #9
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Calling a function for the page

    Quote Originally Posted by kows View Post
    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

  10. #10

    Thread Starter
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    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.

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  12. #12
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Calling a function for the page

    Quote Originally Posted by Nightwalker83 View Post
    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

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
  •  



Click Here to Expand Forum to Full Width