Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Function and class

    Hey,
    I'm trying to echo a variable from a different function within the class.
    How can I do this?
    This is what I've got, just for a quick example:
    PHP Code:
    class UserPermissions
    {
        
    //Post Thread permission
        
    function Permissions()
        {
            
    $test "Hey"//The variable
        
    }
        function 
    PostThread()
        {
            echo 
    $test//Now I want to echo the variable above
        
    }

    Thanks!

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    class UserPermissions
    {

        private 
    $test// can also be `public` or `protected`

        //Post Thread permission
        
    function Permissions()
        {
            
    $this->test "Hey";
        }

        function 
    PostThread()
        {
            echo 
    $this->test;
        }



  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hmm, doesn't seem to echo it.
    I'll mess around with it though if you don't know the problem.

    Thanks.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    the class looks all right.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I don't know the problem because we haven't seen any code aside from the actual class. Let's see something.
    Last edited by kbluhm; 02-02-2010 at 05:18 AM.

  • #6
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Ok:
    $user_permission = new UserPermissions();
    $user_permission->PostThread();

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Quote Originally Posted by Crisp View Post
    Hmm, doesn't seem to echo it.
    I'll mess around with it though if you don't know the problem.

    Thanks.
    You need to call those functions from an object of that class.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by Crisp View Post
    Ok:
    $user_permission = new UserPermissions();
    $user_permission->PostThread();
    Where are you calling the Permissions() method? Since that is the method assigning the value, if you don't call that the value will not be asigned.

  • Users who have thanked kbluhm for this post:

    Crisp (02-02-2010)

  • #9
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    @abduraooft I'm not really sure what you mean.
    I'm not to good at classes yet because I'm still learning them and am kind of new to them.

    Anyways, thanks for the reply,

    @kb, oh dang, I forgot to do that!
    Let me try it now.

  • #10
    New Coder
    Join Date
    Feb 2009
    Posts
    96
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Alright, it works now!
    Thanks man!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •