在 PHP 类中获取数组长度

Gre*_*try 5 php

我是 PHP 面向对象编程的新手。我制作了一个带有数组属性的简单订单类。方法 orderLength 不起作用。我收到一个错误:

调用未定义的方法 Order::count()

PHP:

<?php
    class Order {
        private $order = array();

        public function setOrder($wert) {
            foreach($wert as $value) {
                $this -> order[] = $value;
            }
        }

        public function orderLength() {
            $length = $this -> count(order);
            return $length;
        }

        public function returnOrder() {
            $value = $this -> order;
            return $value;
        }
    }

    $order = new Order;
    $order -> setOrder(array('Book1', 'Book2', 'Book3', 'Book4'));

    foreach ($order->returnOrder() as $value) {
        echo $value . " <br/>";
    }

    echo "The order length is: " . $order->orderLength();
Run Code Online (Sandbox Code Playgroud)

hsz*_*hsz 7

而不是$this->count(order)你可以尝试:

$length = count($this->order);
Run Code Online (Sandbox Code Playgroud)