Vlades Posted January 22, 2014 Share Posted January 22, 2014 (edited) Hello, Is there any way to disallow the use of "clone " for an object? What I mean is, let's say I have a class named "user" I am able to clone the object ; $student = new user(); $teacher = clone $student; Is there any keyword that does not allow me to clone the "$student" object? Thank you in advance Edited January 22, 2014 by Vlades Quote Link to comment https://forums.phpfreaks.com/topic/285584-disallow-cloning-in-php/ Share on other sites More sharing options...
gristoi Posted January 22, 2014 Share Posted January 22, 2014 public function __clone() { throw new Exception( "you are not allowed to clone this object" ); } Quote Link to comment https://forums.phpfreaks.com/topic/285584-disallow-cloning-in-php/#findComment-1466176 Share on other sites More sharing options...
Vlades Posted January 22, 2014 Author Share Posted January 22, 2014 I see, it is the only way?Is there any keyword that locks the object and does not allow it to clone? Quote Link to comment https://forums.phpfreaks.com/topic/285584-disallow-cloning-in-php/#findComment-1466181 Share on other sites More sharing options...
ignace Posted January 22, 2014 Share Posted January 22, 2014 (edited) Make it private. class IamNotDolly { private function __clone() {} } Edited January 22, 2014 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/285584-disallow-cloning-in-php/#findComment-1466184 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.