Braxton Posted May 11, 2012 Share Posted May 11, 2012 I am trying to familiarize myself with using classes and I have a question regarding the use of __construct and __destruct. When using both, should the functions be public or private? Quote Link to comment https://forums.phpfreaks.com/topic/262385-using-__construct-__destruct/ Share on other sites More sharing options...
requinix Posted May 11, 2012 Share Posted May 11, 2012 __destruct() needs to be public so PHP can call it. __construct() can be whatever you want. If public then anybody can instantiate the object; if protected then only it and its children can instantiate it (or in the case of child objects, call it in their constructors); if private then only the object can instantiate itself. Quote Link to comment https://forums.phpfreaks.com/topic/262385-using-__construct-__destruct/#findComment-1344692 Share on other sites More sharing options...
Braxton Posted May 11, 2012 Author Share Posted May 11, 2012 __destruct() needs to be public so PHP can call it. __construct() can be whatever you want. If public then anybody can instantiate the object; if protected then only it and its children can instantiate it (or in the case of child objects, call it in their constructors); if private then only the object can instantiate itself. Thank you so much for your explanation. Quote Link to comment https://forums.phpfreaks.com/topic/262385-using-__construct-__destruct/#findComment-1344893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.