php 访问修改者:公共与私人

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 访问修改者:公共与私人相关的知识,希望对你有一定的参考价值。



<?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() ;


以上是关于php 访问修改者:公共与私人的主要内容,如果未能解决你的问题,请参考以下文章