问题
Can't we use php magic getter setter for doctrine2 entity classes instead of making getter setter for every property of class?
This is my entity class written for doctrine2 to map with table in database.
<?php
namespace Entities;
use Doctrine\ORM\Mapping AS ORM;
/**
* @ORM\Entity
*/
class blogs
{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=11)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=1024, nullable=true)
*/
private $url;
/**
* @ORM\Column(type="string", length=512, nullable=false)
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $blog_data;
/**
* @ORM\OneToMany(targetEntity="Entities\likes", mappedBy="blogs")
*/
private $likeses;
/**
* @ORM\OneToMany(targetEntity="Entities\share", mappedBy="blogs")
*/
private $shares;
/**
* @ORM\OneToMany(targetEntity="Entities\comments", mappedBy="blogs")
*/
private $commentses;
/**
* @ORM\ManyToOne(targetEntity="Entities\ilook_user", inversedBy="blogses")
* @ORM\JoinColumn(name="blog_writer_id", referencedColumnName="id", nullable=false)
*/
private $ilook_user;
/**
* @ORM\ManyToOne(targetEntity="Entities\blog_categories", inversedBy="blogses")
* @ORM\JoinColumn(name="blog_categories_id", referencedColumnName="id", nullable=false)
*/
private $blog_categories;
public function setId($id){
$this->id = $id;
}
public function getId(){
return $this->id;
}
public function setUrl($url){
$this->url=$url;
}
public function getUrl(){
return $this->url;
}
public function setTitle($title){
$this->title = $title;
}
public function getTitle(){
return $this->title;
}
public function setBlog_data($blog_data){
$this->blog_data = $blog_data;
}
public function getBlog_data(){
return $this->blog_data;
}
public function setLikeses($likeses){
$this->likeses=$likeses;
}
public function getLikeses(){
return $this->likeses;
}
public function setShares($shares){
$this->shares = $shares;
}
public function getShares(){
return $this->shares;
}
public function setCommentses($commentses){
$this->commentses = $commentses;
}
public function getCommentses(){
return $this->commentses;
}
public function setIlook_user($ilook_user){
$this->ilook_user = $ilook_user;
}
public function getIlook_user(){
return $this->ilook_user;
}
public function setBlog_categories($blog_categories){
$this->blog_categories = $blog_categories;
}
public function getBlog_categories(){
return $this->blog_categories;
}
}
回答1:
See this example with the magics methods __set
and __get
:
<?php
class PropertyTest
{
private $a, $b;
public function __set($name, $value)
{
echo "Set '$name' to '$value'\n";
$this->$name = $value;
}
public function __get($name)
{
echo "Get $name \n";
return $this->$name;
}
}
echo "<pre>\n";
$obj = new PropertyTest;
$obj->a = 1;
echo $obj->a . "\n\n";
echo "<pre>\n";
$obj = new PropertyTest;
$obj->b = "Hello";
echo $obj->b . "\n\n";
?>
You can read more at:
http://php.net/manual/en/language.oop5.overloading.php#object.set
Edit with other example using the magic method __call
:
<?php
class Called {
private $Id, $Name;
public function __call($name, $arguments) {
$action = substr($name, 0, 3);
$field = substr($name, 3);
if($action == 'set') {
$this->$field = $arguments[0];
}
if($action == 'get') {
return $this->$field;
}
}
}
$obj = new Called();
$obj->setId(4);
echo "<br/>\n";
echo $obj->getId();
echo "<br/>\n";
$obj->setName('Peter');
echo "<br/>\n";
echo $obj->getName();
?>
you can read more at:
http://php.net/manual/en/language.oop5.overloading.php#object.call
来源:https://stackoverflow.com/questions/28846961/how-have-getters-and-setters-for-every-property-of-class