Jump to content

array_search() From Static Property


livethedead

Recommended Posts

Alright so, firstly this script serves no real purpose other than me practicing OOP. The problem is, I want to get the key of the value _input_word from Library::_translators. I can't seem to figure out why its not working I've been trying to debug it and figured out the problem is occurring at the array_search function. Theres no actual error though because it all executes but I am not getting a key. On a side note I'm sure I horridly used exceptions wrong so feel free to point out any suggestions there if you'd like. I realize I could of wrote this much much easier structurally but like I said, for practice.

 

Here's the problem section:

	private function makeTranslation() {
		$check = getCheck();
		if ($check = true) {
			$this->_key = array_search(getInput(), Library::$_translators);
			$this->_translation = Library::$_translations[$this->_key];
		}	else {
				try {
					throw new Exception("fuuu");
				}	catch(Exception $e) {
						echo $e->getMessage();
					}
			}
	}

 

 

Here's the whole code.

 

<?php

abstract class Library {
	static $_translators = array("what the fuck", "thank you",
	"see ya");

	static $_translations = array("wtf", "ty", "cya");	
}

class Input extends Library {
	private $_input_word;
	private $_check;

	function __construct($input) {
		if (in_array($input, Library::$_translators)) {
			$this->_input_word = $input;
			$this->_check = true;
		}	else {
				try {
					if (!$this->_check) {
						throw new Exception("Word not found in library.");
					}
				}		
					catch(Exception $e) {
					echo $e->getMessage();
					}


			}
	}

	protected function getInput() {
		return $this->_input_word;
	}

	protected function getCheck() {
		return $this->_check;
	}
}	



abstract class Translation extends Input {
	private $_translation;
	private $_key;

	private function makeTranslation() {
		$check = getCheck();
		if ($check = true) {
			$this->_key = array_search(getInput(), Library::$_translators);
			$this->_translation = Library::$_translations["{$key}"];
		}	else {
				try {
					throw new Exception("fuuu");
				}	catch(Exception $e) {
						echo $e->getMessage();
					}
			}
	}

	protected function getTranslation() {
		return $this->_translation;
	}
}



final class Output extends Translation {
	private $_trans_output;

	public function makeTranslation() {
		parent::makeTranslation();
		$this->_trans_output = parent::getTranslation();
	}


	public function getTranslation() {
		return $this->_trans_output;
	}

}

?>

Link to comment
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.