Jump to content

Recommended Posts

I write for example class Text:

 

class Text {
    public static function test() {
        return 'test text';
    }
}

 

Today I found this code:

 

class Text {
     
      /** 
       * Protected constructor since this is a static class.
       *
       * @access  protected
       */  
    protected function __construct(){
        // Nothing here
    }

    public static function test() {
        return 'test text';
    }
}

 

How much appropriate is to write this?

 

    /** 
       * Protected constructor since this is a static class.
       *
       * @access  protected
       */  
    protected function __construct(){
        // Nothing here
    }

Link to comment
https://forums.phpfreaks.com/topic/256005-static-class/
Share on other sites

The purpose of a private or protected constructor is to prevent the class from being instantiated from outside of the class. It can give you control over how the constructor() method is instantiated. In this case it would defer statically calling the constructor. But of course with this small snippet of a class, it doesn't make any difference.

Link to comment
https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312435
Share on other sites

For the record, this is called the singleton pattern. It works like this

class Singleton
{
public static $instance;

private function __construct() { }

public static function getInstance()
{
	if (!self::$instance)
		self::$instance = new Singleton;

	return self::$instance;
}
}

$singleton = Singleton::getInstance();

 

Basically, you make the constructor static which prevents it from being instantiated outside of the class. Then, by calling the getInstance method, you instantiate the class internally and then return it. Since the $instance variable is static, this can only happen once. If you call the getInstance method again it simply returns the instance instead of instantiating it multiple times.

 

 

Link to comment
https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312466
Share on other sites

Slight quibble - a Singleton is defined by the fact that it has a static method return (or create THEN return) an instance of its own class.  Merely having a protected/private constructor in a class with static methods does NOT make it a Singleton (nor imply that intent).  Static utility classes that don't need to retain state/utilize an instance is a common design pattern, to the extent that at least one language (C#) allows one to define an entire class as static, which forces the class itself to ONLY contain static methods. 

 

Regardless, static classes and Singletons should be used with care as they ignore scope and encapsulation.  There's usually a better way to solve a problem than to use one of them.

Link to comment
https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312472
Share on other sites

Slight quibble - a Singleton is defined by the fact that it has a static method return (or create THEN return) an instance of its own class.  Merely having a protected/private constructor in a class with static methods does NOT make it a Singleton (nor imply that intent).

 

True. Thanks for pointing that out.

Link to comment
https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312475
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.