Wyświetlanie zawartości zmiennej

0

Witam, mam taki kod:

<?php
	//PHP as a template language
	/*$topic = 'PHP as a template language';
	$helloWorld = 'Hello World';*/
	class Statement{
		private $topic = 'PHP as a template language';
		private $helloWorld = 'Hello World';

		public function getTopic($topic){
			return $this->topic = $topic;
		}
		public function getHelloWorld($helloWorld){
			return $this->helloWorld = $helloWorld;
		}
	}
	$obj = new Statement();
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title><?=$obj->getTopic()?></title>
</head>
<body>
	<?=$obj->getHelloWorld()?>
</body>
</html>

Uczę się programowania obiektowego i nie wszystko jeszcze pewnie rozumiem. Jak się pozbyć pojawiającego się ostrzeżenia i notatki? Mógłby ktoś może poprawić? Będę wdzięczny.

Warning: Missing argument 1 for Statement::getHelloWorld(), called in C:\xampp\htdocs\template\index.php on line 26 and defined in C:\xampp\htdocs\template\index.php on line 12

Notice: Undefined variable: helloWorld in C:\xampp\htdocs\template\index.php on line 13

1

Dlaczego Twoje metody getTopic oraz getHelloWorld zmieniają wartości pól?
setXyz -> ustawienie wartości pola, getXyz -> zwrócenie wartości pola.

0

Poprawiłem, działa. Czy jest to poprawnie napisane?
PS. Nie rozdzielam warstwy widoku na osobny plik, gdyż do hello world nie ma takiej potrzeby :)

<?php
	//PHP as a template language
	/*$topic = 'PHP as a template language';
	$helloWorld = 'Hello World';*/
	class Statement{
		private $topic = 'PHP as a template language';
		private $helloWorld = 'Hello World';

		public function getTopic(){
			return $this->topic;
		}
		public function getHelloWorld(){
			return $this->helloWorld;
		}
	}
	$obj = new Statement();
?>

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title><?=$obj->getTopic()?></title>
</head>
<body>
	<?=$obj->getHelloWorld()?>
</body>
</html>
0

Raczej "poprawnie", ale to całkowicie bezsensowna klasa.

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