【问题标题】:OOP in PHP, pass variable & output directly with function namePHP中的OOP,使用函数名直接传递变量和输出
【发布时间】:2017-01-20 11:23:13
【问题描述】:

我是 OOP 的新手。我创建了这段代码,我知道如何用这种方式调用它

$john = new people('Bla', 'bla'); 

并使用$john->boy(); 调用它 但我想直接调用函数并在其上传递特定的(对于男孩.. $name, $beard 对于女孩... $name, $hair, $eyes)变量。

<?php

    class people {

        public $name;
        protected $hair;
        protected $eyes;
        protected $beard;

        public function __constructor($name, $hair, $eyes, $beard) {
            $this->name = $name;
            $this->hair = $hair;
            $this->eyes = $eyes;
            $this->beard = $beard;
        }

        public function getName() {

            return $this->name;
        }

        public function getHair() {

            return $this->hair;
        }

        public function getEyes() {

            return $this-eyes;
        }

        public function getBeard() {

            return $this->beard;
        }

        public static function boy() {

            $name = getName();
            $beard = getBeard();

            echo $name . ' has ' . $beard . ' beard';
        }

        public static function girl() {

            $name = getName();
            $hair = getHair();
            $eyes = getEyes();

            echo $name . ' has ' . $hair . ' hair and ' . $eyes . ' eyes';
        }
    }

并直接用函数名调用&输出:

people::boy('John', 'long');
people::boy('Steve', 'small');
people::girl( 'Eva', 'long', 'blue' );

【问题讨论】:

  • 不能从静态上下文调用非静态方法(使用this->...类必须通过new实例化)
  • 你想要的不需要课程。您只需要一个带参数的函数。例如。 function boy($name, $beard) { // code }
  • __constructor btw 在 OOP 中无效。 RTM php.net/manual/en/language.oop5.decon.php - 老实说,我不知道你从哪里得到的,但我很想知道。
  • 作为 OOP 的新手,我正在学习一些教程,只是更改了一些名称并替换了一些功能 XD

标签: php function class oop


【解决方案1】:

像 ka_lin sad 一样,你不能从静态上下文中调用非静态方法;因此,您可以为该代码执行的操作是:

<?php

class people {
    protected $name;
    protected $hair;
    protected $eyes;
    protected $beard;

    public function __construct($name, $hair, $eyes, $beard) {
        $this->name = $name;
        $this->hair = $hair;
        $this->eyes = $eyes;
        $this->beard = $beard;
    }

    public function getName() {

        return $this->name;
    }

    public function getHair() {

        return $this->hair;
    }

    public function getEyes() {

        return $this->eyes;
    }

    public function getBeard() {

        return $this->beard;
    }

    public static function boy($name, $beard) {
        $boy   = new self($name, null, null, $beard);
        $name  = $boy->getName();
        $beard = $boy->getBeard();

        echo $name . ' has ' . $beard . ' beard' . PHP_EOL;
    }

    public static function girl($name, $hair, $eyes) {
        $girl = new self($name, $hair, $eyes, null);
        $name = $girl->getName();
        $hair = $girl->getHair();
        $eyes = $girl->getEyes();

        echo $name . ' has ' . $hair . ' hair and ' . $eyes . ' eyes' . PHP_EOL;
    }
}

people::boy('John', 'long');
people::boy('Steve', 'small');
people::girl( 'Eva', 'long', 'blue' );

【讨论】:

  • 这个答案应该包含__constructor不正确的事实。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2011-08-17
  • 2011-10-19
相关资源
最近更新 更多