I'm new to object oriented programming in PHP. I made a simple order class with an array property. The method orderLength is not working. I'm getting an error:
Call to undefined method 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();
您可以尝试使用以下内容代替$this->count(order)
$length = count($this->order);
Just return the count of $order:
public function orderLength() {
return count($this->order);
}
Count是一个方法,而不是类属性,试试这个:
$length = count($this->order);
Simply you miske that line
$this -> count(order);
Your class has no member functions with the name count
, so remove that:
this ->
You have not defined a method count for your class. The way you have done it, it looks like you are calling the count
method of your class, giving order
as an argument.
$length=count($this->order);
Assign the value to the variable $order
and then write the code for getting the count:
$order = $order -> setOrder(array('Book1', 'Book2', 'Book3', 'Book4'));
echo "The order length is: ".$order->orderLength();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.