I am not an expert on static properties or methods, but based on what I have read in other websites I normally use them to accomplish functions that do not require an object instance or to control access to constructors. I use singleton to control the access to the constructor on some of my classes. On the other hand, I use static methods for functions that can be performed without the need to create an instance of the class, such as downloading a file to the client or to sanitize the output to be displayed (remove slashes and converting characters to HTML entities). For example, the singleton method below will allow me to utilize one object instance as many times as I want to, while the other two methods will allow me to perform a quick function without the need to create an instance of an object and then call a method. Hope this helps.
[QUESTION] Based on your post, it seems that you are only interested in creating multiple textareas. That said, have you looked at JavaScript and or JQuery? It could do the same thing, but easier. Also, if you are going to stick with this class, you should make all your methods static and avoid creating an instance of the class all together. Finally, you should incorporate the "...Index()" methods into the code for the other two methods. Just my two cents!
Code: Select all
<?php
class ClassName {
private static $_singleton;
protected function __construct(){}
public static function getInstance(){
try{
if(is_null(self::$_singleton)){
self::$_singleton = new self();
}
if(self::$_singleton instanceof self){
return self::$_singleton;
}
throw Exception("ClassInstanceException");
}catch(Exception $e){
Exception handling code…
}
}
}
class AnotherClassName {
private function __construct(){}
public static function downloadPdfFile($filename, $contents){Your code here...}
public static function sanitizeCodeForDisplay($html){Your code here...}
}
$class1 = ClassName::getInstance(); // First Instance
$class2 = ClassName::getInstance(); // Same as first instance
$class3 = ClassName::getInstance(); // Same as first instance
AnotherClass::downloadPdfFile("helloField", "..."); // I used static because I do not have any need for an object of this class other than this method.
//In addition, this method will always do the same thing whenever I call it, there is nothing specialized about it.
AnotherClass::sanitizeCodeForDisplay("..."); // Same as above, there is no need to create an instance of this class.
?>