Prevent cloning

/**
	 * Prevent cloning of the instance of the Singleton instance.
	 *
	 * @return void
	 */
	private function __clone() {}

	/**
	 * Prevent unserializing of the Singleton instance.
	 *
	 * @return void
	 */
	private function __wakeup() {}

Can some one please help me to understand and comprehend these things:
* Prevent cloning of the instance of the Singleton instance.
* Prevent unserializing of the Singleton instance.

What information did you encounter while thinking about it by yourself, by asking the developer of this code snippet and by having a research on this topic? where did you get stuck?

I will give you an analogy. In various WordPress plugins. the index.php files have this only:
<?php // Silence is golden

This is to ensure that all other files do not have a direct access.

So in our case how ill the cloning if possible harm?

Not exactly. It is so when an HTTP request is made to the folder that file is in without requesting a particular file, that file will be accessed - and output nothing.

This is a security safety measure so that in case “Indexes” is enabled Apache won’t display a list of the files in the folder. (considering WordPress is open source the effort seems rather pointless, meh!)

This page can help:
http://php.net/manual/en/language.oop5.magic.php

4 Likes

To reiterate what @Mittineague has already said. It’s pretty much just there to prevent people from snooping around where they shouldn’t be. Since the main plugins don’t really need to be index.php files, it is often used as a safety precaution.

2 Likes

Singletons are very bad practice anyway and it looks like these have been added to slightly mitigate the negative effects of them.

However, this is bad practice in the same way that singletons are in the first place:

An object should not be in control of whether or not it can be cloned, it should be up to the calling code. By including these you make a lot of assumptions about the environment in which the code is running and limit the flexibility of the code in the process.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.