jcadima
7/27/2017 - 4:52 PM

ACCESS MODIFIERS: public vs private

ACCESS MODIFIERS: public vs private



<?php
/*
============= ACCESS MODIFIERS: public vs private ==============

Setters set values of private properties
Getters get values of private properties

We need access modifiers to limit access from outside the classes that contain them, in order to interact with private method/properties we need to provide public methods

*/

class User {
	private $firstName;
	
	public function setName( $fname ) {
		$this->firstName = $fname ;
	}
	
	public function getName() {
		echo $this->firstName; 
	}
	
}

$user1 = new User() ;
// $user1->firstName = 'Marco'; // Fatal error: cannot access private property
$user1->setName("Toto omoto") ;
$user1->getName() ;