magiczne php

0

Witam mam problem z metodą __get().

Kod:

	abstract class Properties {
		
		function __get($property) {
			echo "W __get()";
			$method = "get{$property}";
			
			if (method_exists($this, $method)) {
				return $this->$method;
			}
		}
		
		function __isset($property) {
			$method = "get{$property}";
			return method_exists($this, $method);
		}

		
		function __set($property, $value) {
			echo "W  __set()";
			$method = "set{$property}";
			if (method_exists($this, $method)) {
				
				return $this->$method($value);
				
			}
		}
		
		
		function __unset($property) {
			$method = "set{$property}";
			if (method_exists($this, $method)) {
				return $this->$method(null);
			}
		}

	}
	
	class Foo extends Properties {
		private $prop;
		
		public function setProp($value) {
			$this->prop = $value;
		}
		
		public function getProp() {
                        echo "W getProp()";
			return $this->prop;
		}
		
		public function sayHello() {
			echo "I am saying hello!";
		}
	}

	$obj = new Foo();
	
	echo $obj->sayHello();
	$obj->prop = "Hello World";
	print $obj->prop;
	print $obj->getProp();


Wynik działania:

I am saying hello!W __set()W __get()W __get()W getProp()Hello World

Albom ślepy i jakaś literówka, albo nie wiem co jest nie tak, czemu __get() nie spełnia swojego zadania?

Pozdrawiam

Brakowało głupich nawiasów, eh... sorry za syf

0

to sie robi inaczej:

protected $_tablica = array();

//...

public function __get($key) { return $this->_tablica[$key]; }

a do wolania funkcji jest bodajze __call

0

no tak, ale to jakbym używał do przechowywania właściwości obiektu tablicy.

1 użytkowników online, w tym zalogowanych: 0, gości: 1