RPeraltaJr
2/23/2018 - 9:20 PM

Getters and Setters

<?php include("class_lib.php"); ?>

<?php 
  // create your object(s)
	$stefan = new person();
	$jimmy = new person;
  
  // set names
	$stefan->set_name("Stefan Mischook");
	$jimmy->set_name("Nick Waddles");
  
  // get names
	echo "Stefan's full name: " . $stefan->get_name();
	echo "Nick's full name: " . $jimmy->get_name(); 
	
	// directly accessing properties in a class is a no-no.
	echo "Stefan's full name: ".$stefan->name;
	
	// correct 
	echo "Stefan's full name: ".$stefan->get_name();
?>
<?php 

class person {
	var $name; 
	
	function __construct($persons_name) {		
		$this->name = $persons_name;		
	}
		
	function set_name($new_name) { 
		$this->name = $new_name;  
 	}

 	function get_name() {
		return $this->name;
	}
	
} 

?>