SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Can I still call object if add function, it's not method of object?

    I have a class as below, assuming this is an object:

    PHP Code:
    class house {
            private 
    $windows;
            
            function 
    __construct() {
            
            }
            
            function 
    setWindows() {
            
            }
            
            function 
    getWindows() {
            
            }
            
            function 
    countWindows() {
                
    // this is object's method
            
    }
        } 
    if I add a function like this:

    PHP Code:
    class house {
            private 
    $windows;
            
            function 
    __construct() {
            
            }
            
            function 
    setWindows() {
            
            }
            
            function 
    getWindows() {
            
            }
            
            function 
    countWindows() {
                
    // this is object's method
                // call isGlassWindows() function here
            
    }
            
            function 
    isGlassWindows () {
                
    // this is not object's method
            
    }
        } 
    Can I call "house class" is an object?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    No you can not, you have to pass the object as a parameter to your function.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    No you can not, you have to pass the object as a parameter to your function.
    But, if this is an object's method, should I use like this?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by greedyman View Post
    But, if this is an object's method, should I use like this?
    I don't know what you want to do so I'm afraid can't answer that question. Could you explain a bit more?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    I don't know what you want to do so I'm afraid can't answer that question. Could you explain a bit more?
    I know an object have properties and methods, right. So I'm only want to know I can add any function (not object's method) for it or not. I see many project they code like #2.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    I'm confused by you saying it's not the object's method. It's a method defined within the object; therefore it is the object's method.

    Scallio: Cant it be called by $this->isGlassWindows() ? (My OOP skills are sorely lacking.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    I'm confused by you saying it's not the object's method. It's a method defined within the object; therefore it is the object's method.

    Scallio: Cant it be called by $this->isGlassWindows() ? (My OOP skills are sorely lacking.)
    isGlassWindows() is a function, assuming that it's not object's method.

    Thank all!

  8. #8
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    My question is what is setWindows going to be doing? This is probably where you will define wether they are glass correct?

  9. #9
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by StarLion View Post
    Scallio: Cant it be called by $this->isGlassWindows() ? (My OOP skills are sorely lacking.)
    Yes, it can

  10. #10
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    My question is what is setWindows going to be doing? This is probably where you will define wether they are glass correct?
    I give an example to everyone to know objects have only properties and methods or not. If I write code with procedure (don't use class), not object. I don't need care properties, methods. Do you know what's object?

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    hmmm .....

    Not sure I have completely understood this, but going back to the OP.

    Quote Originally Posted by greedyman
    Can I call "house class" is an object?
    Is my object really an instance of a house? Use the function is_a()

    PHP Code:
    $b = new house;

    if( 
    is_a($b"house") ) {
    echo 
    'yep, $b is an object of the house class';



  12. #12
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    hmmm .....

    Not sure I have completely understood this, but going back to the OP.



    Is my object really an instance of a house? Use the function is_a()

    PHP Code:
    $b = new house;

    if( 
    is_a($b"house") ) {
    echo 
    'yep, $b is an object of the house class';


    Interesting, I didn't know about is_a(). I've always used instanceOf myself:
    PHP Code:

    <?php
    $myHouse 
    = new House();

    if (
    $myHouse instanceof House) {
        echo 
    'Yes, $myHouse is a House';
    }
    ?>

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I only know of its existence through using simpleTests' assertIsA() function.

    Blimey, did not know it was deprecated and then subsequently that decision was rescinded. instance of or is_a().

    Anyhow, horses for courses -- but was that the OPs real question do you think?

    Or maybe they are looking for method_exists().

  14. #14
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Any instance of "house" will have the method isGlassWindows, I'm not sure why you think the method will not belong to the object. The ONLY way a method will not belong to the object directly is static methods, which are part of the class, not the object.

    If you showed us an actual working example of what you are asking, I could give you more answers but in the example you've provided, isGlassWindows will belong to the object.


Bookmarks

Posting Permissions

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