Lance 'cevap üzerinde durmak; sınıfın noktası verileri için bir kap başka bir şey olmak için ise, veri ile bir şey yapıyor yerine oldukça güvendesiniz. Ama OOP iyi bir temel encapsulation sadık olmaktır. Encapsulation Eğer dışarıdan gelen nesnenin iç ayrıntılarını gizlemek ve arabirim yöntemleri var ile yalnızca dış erişim alanları izin verdiğini, diğer şeyler arasında, anlamına gelir.
Diyelim Kullanıcı nesne alanları dışarıdan değiştirilemez istemiyorum diyelim, ama sadece erişilebilir, daha sonra aşağıdaki gibi bir şey ile daha iyi olacak you'ld:
class User
{
private $_userId;
// and a bunch of other fields
public function __construct( $data )
{
// do something with the data
}
public function getUserId()
{
return $this->_userId;
}
// and a bunch of other getters to access the data
}
Tüm dürüstlük, sen magic methods ne istediğiniz taklit ve __ set yönteminde herhangi bir istenmeyen değiştirilmesini yakalamak için kümesi olsun __ gibi ve __ kullanabilirsiniz.
Ayrıca, ben küresel bir değişken olarak oturumu kullanmak olmaz. (Ben örnekte gösterildiği gibi) Bunu yapıcısına bir argüman olarak oturum nesnesi geçmelidir. Bu gevşek bağlantı zorlar. Çünkü artık Kullanıcı nesneleri global oturum nesnesine bağlı olan, ancak herhangi bir veri içeri geçebileceğini kurucusuna geçirmeden ile sınıf daha esnek hale getirir.
Edit:
Here's an example of how you could pass an object (for instance your session object) to the constructor. One thing to keep in mind is that, the way your session object is designed, it still, somewhat, enforces tight coupling, because it mandates getting properties through the get() method.
class User
{
public function __construct( $data )
{
$this->_id = $data->get( 'id' );
$this->_firstname = $data->get( 'firstname' );
// etc
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session );
Sen gevşek bağlantı yaymak için eldeki birkaç seçenek var ve kullanıcı biraz daha esnek nesne yapıyor.
: Eğer kullanıcı nesnesi için veri olarak sağlandığını Mandası
distinct arguments
class User
{
protected $_id;
protected $_firstname;
// etc;
public function __construct( $id, $firstname, /* etc */ )
{
$this->_id = $id;
$this->_firstname = $firstname;
// etc
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session->get( 'id' ), $session->get( 'firstname' ), /* etc */ );
array
class User
{
protected $_fields = array(
'id' => null,
'firstname' => null,
// etc
);
// dictate (type hint) that the argument should be an array
public function __construct( array $data )
{
foreach( $data as $field => $value )
{
if( array_key_exists( $field, $this->_fields )
{
$this->_fields[ $field ] = $value;
}
}
}
}
// usage
$session = new YourSessionObject();
$array = /* fill this array with your session data */;
$user = new User( $array );
implementing some interface
// objects that implement this interface need to implement the toArray() method
interface Arrayable
{
public function toArray();
}
class User
{
protected $_fields = array(
'id' => null,
'firstname' => null,
// etc
);
// dictate (type hint) that the argument should implement Arrayable interface
public function __construct( Arrayable $data )
{
// get the data by calling the toArray() method of the $data object
$data = $data->toArray();
foreach( $data as $field => $value )
{
if( array_key_exists( $field, $this->_fields )
{
$this->_fields[ $field ] = $value;
}
}
}
}
class YourSessionObject implements Arrayable
{
public function toArray()
{
/* this method should return an array of it's data */
}
}
// usage
$session = new YourSessionObject();
$user = new User( $session );
etc
Orada birkaç diğer seçenekleri vardır, ancak bu size bazı fikirler verecektir. Umarım bu yardımcı olur.