PHP Static Class Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
artofwork
Forum Newbie
Posts: 7
Joined: Wed Feb 08, 2012 1:28 am

PHP Static Class Problem

Post by artofwork »

I've been trying to find a solution all day with this code i wrote, besides using a for loop and passing numerical values to this class which i'm trying to avoid, I guess I just don't fully understand the concept of a static class.

This class generates a textarea and removes it aswell, my goal is to generate more then 1 textarea hence why im using a class, I mean could just easily write it as a function, but its really not the point.

Was wondering if someone could have a look and tell me what the heck im doing wrong, not looking for you to write it for me, just a clear explanation of why this isn't working like I want it to.

Code: Select all

<?php

class CreateField
{
	private static $field = array();
	private static $index = 0;
	
	function __construct($_POST)
	{
		switch($_POST)
		{
			case 'add':
				self::createField();
				break;
			case 'sub':
				self::destroyField();
				break;
			default:
				break;
		}
	}

	
	private static function createField()
	{
		self::$field[self::addIndex()] = "<textarea name=\"text_".self::$index."\"></textarea><br />";
	}
	
	private static function destroyField()
	{
		self::$field[self::subIndex()] = " ";
	}
	
	public function getField()
	{
		return self::$field;
	}
	private static function addIndex()
	{
		return self::$index++;
	}
	
	private static function subIndex()
	{
		return self::$index--;
	}
}
?>
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

Re: PHP Static Class Problem

Post by xtiano77 »

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.
?>
Post Reply