www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 03-16-2010, 08:59 PM
    zenthoef zenthoef is offline
    Registered User
     
    Join Date: Dec 2008
    Posts: 50
    Calling Functions from Other Functions

    I'm sure its possible to call functions from other functions, but it is not working for me. Is there some syntax someone can provide to me as an example of calling a function within another function?

    Also, both of these functions are in the same class. I have not declared any functions in this class as public or private. Do functions in a class default to private? Or do functions default to public? If they are public by default, are functions in the same class in each other's scope?

    Thanks!
    Reply With Quote
      #2  
    Old 03-16-2010, 09:18 PM
    hastx's Avatar
    hastx hastx is offline
    Registered User
     
    Join Date: May 2003
    Posts: 451
    Here is a prior discussion with a little relevance:
    http://www.webdeveloper.com/forum/sh...lling+function

    I think public is default..., but i also think that public and private visibility declaration on functions is only available as of php 5, and a lot of people still use php 4...so you may not need to declare depending on your target and enough planning.

    There is a good resource on visibility here:
    http://www.webdeveloper.com/forum/sh...lling+function
    __________________
    We do precision guesswork

    Last edited by hastx; 03-16-2010 at 09:39 PM.
    Reply With Quote
      #3  
    Old 03-16-2010, 10:22 PM
    tirna's Avatar
    tirna tirna is offline
    Registered User
     
    Join Date: Mar 2010
    Location: Melbourne, Australia
    Posts: 2,030
    yes, private and public delarations of class variables and functions is available in php5

    Imo it's good practice to declare a variable or function private/public regardless of the default....but that's just me

    To call a function with a class file in php5 use:

    Code:
    $this->functionName(arg1,arg2,argN);

    Last edited by tirna; 03-16-2010 at 10:24 PM.
    Reply With Quote
      #4  
    Old 03-16-2010, 10:58 PM
    zenthoef zenthoef is offline
    Registered User
     
    Join Date: Dec 2008
    Posts: 50
    Thanks for the replies. I don't think I was clear in what I was asking. Basically, is this possible:

    Code:
    class someClass{
    
    function_one($arg1)
    {
      $output_one = $arg1+15;
      return $output_one;
    }
    
    function_two()
    {
      $send = 3;
      $output_two = function_one($send);
      return $output_two;
    }
    }
    My question is if i call funtion_two will it return a value of 18? If this is possible is my syntax right? I'm basically doing this, but more complicated and its not working.

    Thanks.
    Reply With Quote
      #5  
    Old 03-17-2010, 12:20 AM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,727
    A function defined in a class does not "exist" only by its function name, you must reference it via the "->" operator as a member of an object, or statically via the "::" operator as a class member. Therefore in your example you would want to use the special "$this" variable to indicate that the function is a method of the current object:
    PHP Code:
    $output_two = $this->function_one($send);
    __________________
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    Kindle Minds (blog about Amazon Kindle)
    Reply With Quote
      #6  
    Old 03-17-2010, 04:17 AM
    tirna's Avatar
    tirna tirna is offline
    Registered User
     
    Join Date: Mar 2010
    Location: Melbourne, Australia
    Posts: 2,030
    Quote:
    Originally Posted by NogDog View Post
    A function defined in a class does not "exist" only by its function name, you must reference it via the "->" operator as a member of an object, or statically via the "::" operator as a class member. Therefore in your example you would want to use the special "$this" variable to indicate that the function is a method of the current object:
    PHP Code:
    $output_two = $this->function_one($send);
    Correct me if I'm wrong, but isn't the above what was said earlier

    Code:
     
     
    yes, private and public delarations of class variables and functions is available in php5
    
    Imo it's good practice to declare a variable or function private/public regardless of the default....but that's just me :)
    
    To call a function with a class file in php5 use:
    
    
    Code:
    $this->functionName(arg1,arg2,argN);
    Reply With Quote
      #7  
    Old 03-17-2010, 12:29 PM
    NogDog's Avatar
    NogDog NogDog is online now
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 14,727
    Quote:
    Originally Posted by tirna View Post
    Correct me if I'm wrong, but isn't the above what was said earlier
    Yes, it was said, but apparently not understood, so I tried again in the hopes that this time it might be.
    __________________
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    Kindle Minds (blog about Amazon Kindle)
    Reply With Quote
      #8  
    Old 03-17-2010, 06:47 PM
    zenthoef zenthoef is offline
    Registered User
     
    Join Date: Dec 2008
    Posts: 50
    I did misunderstand that post! However, the "$this->" solved my problems. Thanks so much for all who answered!
    Reply With Quote
    Reply

    Bookmarks

    Tags
    class, functions, php


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:16 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.