dongyu4908 2016-02-14 05:59
浏览 49
已采纳

PHP OOP构建探索不清楚

This is not a question about a particular problem, but rather a question asking for advice and / or assistance.

Im a 2nd year student, Im really struggling to get the hang of OOP programming, the fact that my textbook is not very clear, in its explanation is not helping either. I know there are 100's of examples on the web of this probably but I would like to focus specifically on the example used in my textbook.

The introduction to OOP in php starts with this: EXAMPLE 1

class Person{
var $name;
function set_name($data){
    $this->name=$data;
  }

function get_name(){
    return $this->name;
  } 
}
$ralph = new Person;
$ralph->set_name('Ralph');
echo 'friend name is '.$ralph->get_name();

Fine I get that no problem However it then goes on giving, what is in my view a very brief explanation on constructors, saying:

Wouldn't it be nice if you could both create and initialize an object at the same time, PHP allows you to do that with constructors.

And then proceeds to give the following example EXAMPLE 2(UNCLEAR...?)

class Person{
var $name;

function __construct($data)
{ 
  $this->name=$data
}

function set_name($data){
    $this->name=$data;
}

function get_name(){
    return $this->name;
}

}

$dan = new Person;
echo"Friend name is ", $dan->get_name(), ".";

My Question

I would really like to know what is the difference between the two examples above? Also if a beginner practical example could be included it will be enormously appreciated!

  • 写回答

3条回答 默认 最新

  • dragon8899 2016-02-14 06:07
    关注

    It's no wonder you are confused, that is a very bad example (looks like they forgot to include passing the name to the constructor)! There are also some outdated styles here as well, typically people do not use var anymore for property declarations but declare their scope (public, private, protected, etc.), also some syntax errors (missing semicolon), but that is for another day.

    What would have made this a lot more clear is if they actually used the new power of the constructor, which for some reason, they did not. Here is an updated example where the constructor is actually being used:

    class Person{
      private $name;
    
      function __construct($data)
      { 
        $this->name=$data;
      }
    
      function set_name($data){
          $this->name=$data;
      }
    
      function get_name(){
          return $this->name;
      }
    
    }
    
    // pass the name to the constructor and you can bypass the set_name call
    $dan = new Person("Timothy");
    
    echo "Friend name is ", $dan->get_name(), ".";
    

    This definitely gets much more useful than passing scalars, but hopefully it illustrates what constructors can do a little bit better. Constructors are called automatically whenever you instantiate a new object from a class. Let me know if you have any questions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址