【问题标题】:please help me with the my OOP PHP doubt?请帮我解决我的 OOP PHP 疑问?
【发布时间】:2010-12-29 14:42:15
【问题描述】:

我有fol prog

class Person {
  var $name;
  function Person () {

  }
}

$fred = new Person;
$fred->name = "Fred";
$barney =& new Person;
$barney->name = "Barney";

echo $barney->name;
echo $fred->name;

两个 echo 语句都给出了正确的相同输出,即“Fred”和“Barney”,所以在声明 $barney 时给出 & 有什么用。这里的“&”指的是什么?

谢谢

【问题讨论】:

  • 这是 PHP4。为什么不使用 PHP5?

标签: php oop this


【解决方案1】:

它将变量作为引用传递,但在 PHP5+ 中不再需要。

【讨论】:

    【解决方案2】:

    PHP4 中的引用 (http://www.php.net/manual/en/oop4.newref.php)

    意思是“通过引用”。它成为同义词,而不是 (further reading) 的副本。此外,编写构造函数的新方法不是使用与类本身相同的名称,而是使用__construct() 函数:

    class Person {
    
      function __construct() {
        // setup shop
      }
    
    }
    

    此外,请务必通过将某些方法和属性指定为“公共”、“私有”、“受保护”等来正确使用 Visiblity

    【讨论】:

    • 猜猜如果我们不写 public 据我所知 PHP 中没有像公共私有访问类型这样的东西吗?
    • 是的,Nishant。 PHP中的OOP有public、private、protected之分。
    【解决方案3】:

    请参阅 PHP 手册中的 References Explained

    您仍然使用 PHP4 有什么特别的原因吗?它不再受支持,PHP5s OOP 更加稳固和强大。

    【讨论】:

    • 那么您可能正在从过时的材料中学习。 PHP4 已经死了。查看 PHP 手册中的 Getting StartedLanguage Reference 章节。说明书其实还不错。
    猜你喜欢
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2022-11-26
    • 2011-09-16
    相关资源
    最近更新 更多