在 PHP 中获取和设置(私有)属性,就像在 C# 中一样,无需使用 getter setter 魔术方法重载

发布于 2024-10-16 04:36:24 字数 1432 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独孤求败 2024-10-23 04:36:24

没有。

但是,使用 __get__set 充当 getName()setName($val) 的动态代理有什么问题> 分别?就像这样:

public function __get($name) { 
    if (method_exists($this, 'get'.$name)) { 
        $method = 'get' . $name; 
        return $this->$method(); 
    } else { 
        throw new OutOfBoundsException('Member is not gettable');
    }
}

这样你就不会将所有内容都填充到一个怪物方法中,但你仍然可以使用 $foo->bar = 'baz'; 语法与私有/受保护的成员变量......

Nope.

However what's wrong with using __get and __set that act as dynamic proxies to getName() and setName($val) respectively? Something like:

public function __get($name) { 
    if (method_exists($this, 'get'.$name)) { 
        $method = 'get' . $name; 
        return $this->$method(); 
    } else { 
        throw new OutOfBoundsException('Member is not gettable');
    }
}

That way you're not stuffing everything into one monster method, but you still can use $foo->bar = 'baz'; syntax with private/protected member variables...

葬心 2024-10-23 04:36:24
ReflectionClass is your salvation

我知道这对亨德拉来说为时已晚,但我相信这对其他人会有帮助。

在 PHP 核心中,我们有一个名为 ReflectionClass 的类,它可以操作对象范围内的所有内容,包括属性和方法的可见性。

在我看来,它是 PHP 有史以来最好的课程之一。

让我举一个例子:

如果你有一个带有私有属性的对象,并且你想从外部修改它,

$reflection = new ReflectionClass($objectOfYourClass);
$prop = $reflection->getProperty("PrivatePropertyName");
$prop->setAccessible(true);
$prop->setValue($objectOfYourClass, "SOME VALUE");
$varFoo = $prop->getValue();

你可以使用方法八来做同样的事情;

我希望我能帮忙;

ReflectionClass is your salvation

I know it's too late for Hendra but i'm sure it will be helpfull for many others.

In PHP core we have a class named ReflectionClass wich can manipulate everything in an object scope including visibility of properties and methods.

It is in my opinion one of the best classes ever in PHP.

Let me show an example:

If you have an object with a private property and u want to modify it from outside

$reflection = new ReflectionClass($objectOfYourClass);
$prop = $reflection->getProperty("PrivatePropertyName");
$prop->setAccessible(true);
$prop->setValue($objectOfYourClass, "SOME VALUE");
$varFoo = $prop->getValue();

This same thing you can do with methods eighter;

I hope i could help;

献世佛 2024-10-23 04:36:24

如果使用神奇属性似乎不正确,正如其他海报已经指出的那样,您还可以考虑 ReflectionClass::getPropertyReflectionProperty::setAccessible

或者在类本身上实现必要的 getter 和 setter 方法。

针对您提出的语言功能问题,我想说动态类型语言与静态类型语言不同是预料之中的。每种具有 OOP 的编程语言的实现方式都略有不同:面向对象语言:比较

If using magical properties doesn't seem right then, as already pointed out by other posters, you can also consider ReflectionClass::getProperty and ReflectionProperty::setAccessible.

Or implement the necessary getter and setter methods on the class itself.

In response to the language features issue that you raised, I'd say that having a dynamically typed language differ from a statically typed one is expected. Every programming language that has OOP implements it somewhat differently: Object-Oriented Languages: A Comparison.

停顿的约定 2024-10-23 04:36:24
class conf_server
{

private $m_servidor="localhost";
private $m_usuario = "luis";
private $m_contrasena = "luis";
private $m_basededatos = "database";

public function getServer(){
    return $this->m_servidor;
}
public function setServer($server){
    $this->m_servidor=$server;
}
public function getUsuario(){
    return $this->m_usuario;
}
public function setUsuario($user){
    $this->m_usuario=$user;
}
public function getContrasena(){
    return $this->m_contrasena;
}
public function setContrasena($password){
    $this->m_contrasena=$password;
}
public function getBaseDatos(){
    return $this->m_basededatos;
}
public function setBaseDatos($database){
    $this->m_basededatos->$database;
}
}
class conf_server
{

private $m_servidor="localhost";
private $m_usuario = "luis";
private $m_contrasena = "luis";
private $m_basededatos = "database";

public function getServer(){
    return $this->m_servidor;
}
public function setServer($server){
    $this->m_servidor=$server;
}
public function getUsuario(){
    return $this->m_usuario;
}
public function setUsuario($user){
    $this->m_usuario=$user;
}
public function getContrasena(){
    return $this->m_contrasena;
}
public function setContrasena($password){
    $this->m_contrasena=$password;
}
public function getBaseDatos(){
    return $this->m_basededatos;
}
public function setBaseDatos($database){
    $this->m_basededatos->$database;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文