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 4 of 4
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    Is it possible to create an object but not call the method __construct?

    Is it possible to just call an object but not run its __construct method? In other words:
    PHP Code:
    class my_class {
    function 
    __construct() {
    echo 
    'It has been constructed';
    // Content here
    }
    //* Other methods
    }
    $v = new my_class
    Is it possible to just make it so that it won't run the data within the body of the method (or function?) __construct but still have all of its methods?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You may use some conditions to skip the contents of your function, like

    PHP Code:
    class my_class {
    function 
    __construct($value=true) {
    if(
    $value==false) return;
    echo 
    'It has been constructed';
    // Content here
    }
    //* Other methods
    }
    $v = new my_class(false); 
    btw, let me ask, what's your ultimate aim by doing this?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    It's nothing too big - sometimes I want to call an object and run whatever's in its __construct method... while on other times I would want to just call an object but not run it. In my __construct method it just calls and gets a cache from a table of mine. For the "other times" I want to call my object and rebuild the cache.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You know you don't require a __construct at all right? It does cause a problem with reflection, but if you're using new, a constructor is what it will try to find, but will still create the object (I don't really agree with this handling, but that's how zend chose to do it).

    Sadly, with how you've described this you will always need to use some kind of conditional control. If it were something like only performing it on the first call and never again, you can control that a little easier with a static value:
    PHP Code:
    class MyClass
    {
        private static 
    $instances 0;
        public function 
    __construct()
        {
            if (
    self::$instances++ <= 0)
            {
                 
    // Initial load stuffs, we never need to worry about with each object again.
            
    }
        }
        public function 
    __destruct()
        {
            --
    self::$instances;
        }

    Note that this is not like the singleton pattern - __construct cannot be allowed at all in a singleton instance since you cannot reassign the value of $this.

    Is that something like what you're asking to do?

    Edit:
    Oh yes, and another idea too.
    Leave the construct for an object, but move what you want to do to another method. That lets you control the call on the caller's side instead of the class' side. This is probably my recommendation, but without knowing the control scheme I can't really suggest anything else.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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