为什么有两种不同的方式来显示类方法的结果?PHP

为什么有两种不同的方式来显示类方法的结果?PHP,php,Php,我发现有两种不同的方式可以显示类方法的结果。一种是使用对象操作符,另一种是将方法作为函数调用。我想知道为什么,什么是最好的 采取以下行动: <!DOCTYPE html> <html> <head> <title>Cats</title> <link type='text/css' rel='stylesheet' href='style.css'/> </head>

我发现有两种不同的方式可以显示类方法的结果。一种是使用对象操作符,另一种是将方法作为函数调用。我想知道为什么,什么是最好的

采取以下行动:

<!DOCTYPE html>
<html>
    <head>
      <title>Cats</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
    </head>
    <body>
      <p>
        <?php
        class Cat {
            public $isAlive = true;
            public $numLegs = 4;
            public $name;
            public function __construct($name) {
                $this->name = $name;    
            }
            public function meow() {
               return "Meio meow";
            } 
        }
        $Cat1 = new Cat(CodeCat);
        echo $Cat1->meow;
        ?>
      </p>
    </body>
</html>
什么是交易?有什么更好的 谢谢回复!
-Adrian

好,让我们从使用一些实际运行的代码开始

<?php
class Cat {
    public $isAlive = true;
    public $numLegs = 4;
    public $name;
    public function __construct($name) {
        $this->name = $name;    
    }
    public function meow() {
       return "Meio meow";
    } 
}
?>
<!DOCTYPE html>
<html>
<head>
  <title>Cats</title>
  <link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
      <p>
<?php
    $Cat1 = new Cat('Fluffy');
    echo $Cat1->meow();
    echo $Cat1->name;
?>
      </p>
</body>
</html>
但是,如果您有一个不希望开发人员在无法首先检查值的情况下处理的属性,您可以将此新属性定义为private,例如“private$owner;”

现在不允许使用以下语法:

$Cat1->owner = 'Adrian';
因为它是一个私有属性,但您仍然希望类的用户能够为owner属性设置值,因此您可以创建一个公共方法,如meow方法,该方法将设置此私有属性的值,但前提是它通过了一些必需的测试,如:

<?php
    class Cat {
        public $isAlive = true;
        public $numLegs = 4;
        public $name;

        private $owner;

        public function __construct($name) {
            $this->name = $name;    
        }
        public function meow() {
           return "Meio meow";
        } 

        public function setOwner($var) {
            if ( $var == 'Adrian' || $var == 'Rocky' ) {
               $this->owner = $var;
            } else {
               $this->owner = 'Unknown';
               throw new Exception( 'Invalid owners name' );
            }
        }
    }
?>
因此,您创建了一个公共方法来允许访问您的私有属性,但前提是它满足某些特定条件

它基本上是一种控制对需要特定值的属性的访问的方法

我建议你提供文件
因为还有一个受保护的限定符以及公共和私人限定符。

区别在于echo meow$Cat1;不起作用吗?那么。。。回声喵喵->Cat1美元;我认为你犯了一个错误。喵喵-不是对象这个问题似乎离题了,因为它没有意义,而且两个例子都不起作用。什么是CodeCat?嘿,谢谢你的回答。我稍后会回来看这篇文章,但我只是想让你知道我最终注意到了你的回复。
$Cat1->owner = 'Adrian';
<?php
    class Cat {
        public $isAlive = true;
        public $numLegs = 4;
        public $name;

        private $owner;

        public function __construct($name) {
            $this->name = $name;    
        }
        public function meow() {
           return "Meio meow";
        } 

        public function setOwner($var) {
            if ( $var == 'Adrian' || $var == 'Rocky' ) {
               $this->owner = $var;
            } else {
               $this->owner = 'Unknown';
               throw new Exception( 'Invalid owners name' );
            }
        }
    }
?>