在PHP中为对象属性分配动态值 [英] Assigning dynamic values to object properties in PHP

查看:62
本文介绍了在PHP中为对象属性分配动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用程序一段时间了,试图更好地了解Php中的OOP.从第1平方开始,我有一个快速问题要解决.许多基本示例都显示静态值,例如分配值时的$bob->name = "Robert";.但我想通过以下形式传递动态值:$name = $_POST['name'];

I've used procedural for sometime now and trying to get a better understanding of OOP in Php. Starting at square 1 and I have a quick question ot get this to gel. Many of the basic examples show static values, e.g., $bob->name = "Robert"; when assigning a value. But I want to pass dynamic values, say from a form: $name = $_POST['name'];

class Person { 

    // define properties 
    public $name; 
    public $weight; 
    public $age;

    public function title() { 
        echo $this->name . " has submitted a request "; 
    }
} 

$bob = new Person; 

// want to plug the value in here
$bob->name = $name; 
$bob->title();

我想我在某些方面有些困惑,因为从类,封装和内部访问变量是不可行的. 规则"等,可以$ name = $ _POST ['name'];住在课外的任何地方,还是我错过了重点?

I guess I'm getting a little hung up in some areas as far as accessing variables from within the class, encapsulation & "rules", etc., can $name = $_POST['name']; reside anywhere outside of the class or am I missing an important point?

谢谢

推荐答案

$bob->name = $_POST['name'];

将对象的($bob)name属性设置为$_POST['name']

Set the object's ($bob) name property to $_POST['name']

这篇关于在PHP中为对象属性分配动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆