【问题标题】:php oop some vague stuffphp oop 一些模糊的东西
【发布时间】:2016-06-07 00:17:32
【问题描述】:

关于 php 对象的非常快速的问题。所以这里是代码:

<?php
            class Person {
            public $isAlive = true;
            public $firstname, $lastname, $age;

       public function __construct($firstname, $lastname, $age) {
           $this->firstname = $firstname;
           $this->lastname = $lastname;
           $this->age = $age;
       }
            }

        $teacher = new Person("boring", "12345", "boring");
            echo $teacher->isAlive; 
        $student = new Person("Phil", "Kroupoderov", "20");
            echo $student->age;


         ?>

我的年龄应该显示为 20,但由于某种原因显示为 120。为什么有一个 1 以及如何摆脱它??????

【问题讨论】:

  • true 在 PHP 输出时表示为整数。所以echo $teacher-&gt;isAlive; 会打印1,它会在您打印年龄之前出现。

标签: php oop object


【解决方案1】:

约翰·康德是正确的。你需要改变这个:

    $teacher = new Person("boring", "12345", "boring");
        echo $teacher->isAlive; 
    $student = new Person("Phil", "Kroupoderov", "20");
        echo $student->age;

到这里:

    $teacher = new Person("boring", "12345", "boring");
        echo $teacher->isAlive."\n"; 
    $student = new Person("Phil", "Kroupoderov", "20");
        echo $student->age."\n"; 

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 2015-04-14
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2019-05-16
    相关资源
    最近更新 更多