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 12 of 12
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    135
    Thanks
    13
    Thanked 10 Times in 10 Posts

    OOP class call function inside of self

    hi there

    i am currently learning php, and im struggling with somehting, due to my lack of knowledge.

    how do i call a class function inside the class

    PHP Code:

    class myclass
    {
        function 
    func1()
        {
             return 
    "Hello World!";
        }

        function 
    func2()
        {
             return 
    func1(); //call func1 from this class
        
    }


    this way doesnt seem to work. do i have to instaniate the class first?

    in other languages this normally works.
    Last edited by mike182uk; 01-16-2010 at 12:01 AM.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    you have to call it with $this

    so it would be

    PHP Code:
    class myclass
    {
        function 
    func1()
        {
             return 
    "Hello World!";
        }

        function 
    func2()
        {
             return 
    $this->func1(); //call func1 from this class
        
    }


    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Regular Coder
    Join Date
    May 2008
    Posts
    135
    Thanks
    13
    Thanked 10 Times in 10 Posts
    hey thanks for the quick reply.

    well shouldnt this work?

    PHP Code:
    class myclass
    {
        function 
    func1()
        {
             return 
    "Hello World!";
        }

        function 
    func2()
        {
             return 
    $this->func1(); //call func1 from this class
        
    }

    }  

    echo 
    myclass::func2(); 
    i get an error:

    Code:
    Fatal error: Using $this when not in object context...

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    the :: notation is reserved for static methods/properties. or in other words, to use $this, you need an instance of the class (created with new).

    PHP Code:
    // as of PHP 5
    class myClass
    {
        protected static 
    $text "something";
        protected 
    $otherText   " else";

        public static function 
    doSomething() 
        {
             echo 
    self::$text;
        }

        public function 
    doSomethingElse()
        {
             echo 
    self::$text$this->otherText;
        }
    }

    // echoes "something"
    myClass::doSomething();
    // fails
    myClass::doSomethingElse();

    $mci = new myClass;
    // echoes "something else"
    $mci->doSomethingElse(); 
    Last edited by Dormilich; 01-15-2010 at 11:47 PM.

  • Users who have thanked Dormilich for this post:

    mike182uk (01-15-2010)

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by mike182uk View Post
    hey thanks for the quick reply.

    well shouldnt this work?

    PHP Code:
    class myclass
    {
        function 
    func1()
        {
             return 
    "Hello World!";
        }

        function 
    func2()
        {
             return 
    $this->func1(); //call func1 from this class
        
    }

    }  

    echo 
    myclass::func2(); 
    i get an error:

    Code:
    Fatal error: Using $this when not in object context...
    No try this

    PHP Code:
    class myclass
    {
        function 
    func1()
        {
             return 
    "Hello World!";
        }

        function 
    func2()
        {
             return 
    $this->func1(); //call func1 from this class
        
    }

    }  

    $class = new myclass();
    echo 
    $class->func2(); 
    You can not say you know how to do something, until you can teach it to someone else.

  • Users who have thanked timgolding for this post:

    mike182uk (01-15-2010)

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    This would also work
    PHP Code:
    class myclass
    {
        function 
    func1()
        {
             return 
    "Hello World!";
        }

        function 
    func2()
        {
             return 
    myclass::func1(); //call func1 from this class
        
    }

    }  

    echo 
    myclass::func2(); 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    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 JAY6390 View Post
    This would also work
    PHP Code:
    class myclass
    {
        function 
    func1()
        {
             return 
    "Hello World!";
        }

        function 
    func2()
        {
             return 
    myclass::func1(); //call func1 from this class
        
    }

    }  

    echo 
    myclass::func2(); 
    No, you'd have to name methods as static to call them like that. It'd work in PHP4, but 5 would throw a notice or a warning... don't have time to research it.
    Last edited by kbluhm; 01-15-2010 at 11:53 PM.

  • #8
    Regular Coder
    Join Date
    May 2008
    Posts
    135
    Thanks
    13
    Thanked 10 Times in 10 Posts
    awesome thank you for your help.

    so if i ever want to call something whilst inside the class $this-> always referers to the class?

    so if i define a variable in the class i access it with $this->variable rather than $variable?

  • #9
    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 mike182uk View Post
    awesome thank you for your help.

    so if i ever want to call something whilst inside the class $this-> always referers to the class?

    so if i define a variable in the class i access it with $this->variable rather than $variable?
    Unless you're in a static method, which are out of the scope of $this.

  • #10
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by mike182uk View Post
    awesome thank you for your help.

    so if i ever want to call something whilst inside the class $this-> always referers to the class?

    so if i define a variable in the class i access it with $this->variable rather than $variable?
    Yep
    You can not say you know how to do something, until you can teach it to someone else.

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by mike182uk View Post
    so if i ever want to call something whilst inside the class $this-> always referers to the class?
    it refers to the instance of the class. to refer to the class itself use self:: (inside) or className::

    Quote Originally Posted by mike182uk View Post
    so if i define a variable in the class i access it with $this->variable rather than $variable?
    yes (given that you use the class instance). static properties are called via self::$propertyName or className::$propertyName

  • #12
    Regular Coder
    Join Date
    May 2008
    Posts
    135
    Thanks
    13
    Thanked 10 Times in 10 Posts
    well thank you all for your help on this. i guess i need to go away and brush up in my oop php. i am coming from a javascript / asp background, so things seem a little wierd. thanks again!


  •  

    Posting Permissions

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