[英]PHP Cannot redeclare class
我發現以前的代碼(來自上一個問題)太慢了,無法完成我想要的操作,所以現在我有了一個快速運行的代碼。 我添加了一個簡單的代碼來計數。 至於不能重新聲明類,我已經閱讀了其他問題,這些問題涉及到引用諸如require_once(something.php)之類的.php文件。除此之外,我沒有任何其他文件。 我嘗試設置兩個文件,所以我確實有一個.php文件,但是隨后出現多個錯誤。 這是我的代碼。
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set("log_errors", 0);
$numbers = 0;
$amout = 0;
while ($numbers < 50) {
$numbers + 1;
// This till end of class is to get the divisor numbers
class Divisors {
public $factor = array();
public function __construct($num) {
$this->num = $num;
}
// count number of divisors of a number
public function countDivisors() {
if ($this->num == 1) return 1;
$this->_primefactors();
$array_primes = array_count_values($this->factor);
$divisors = 1;
foreach($array_primes as $power) {
$divisors *= ++$power;
}
return $divisors;
}
// prime factors decomposer
private function _primefactors() {
$this->factor = array();
$run = true;
while($run && @$this->factor[0] != $this->num) {
$run = $this->_getFactors();
}
}
// get all factors of the number
private function _getFactors() {
if($this->num == 1) {
return ;
}
$root = ceil(sqrt($this->num)) + 1;
$i = 2;
while($i <= $root) {
if($this->num % $i == 0) {
$this->factor[] = $i;
$this->num = $this->num / $i;
return true;
}
$i++;
}
$this->factor[] = $this->num;
return false;
}
} // our class ends here
$example = new Divisors($numbers);
// Here it will check if the divisor has 5 divisors
if ($example->countDivisors() == 5) {
// if true it will add 1 to the amount of numbers with 5 divisors
$amount + 1;
}
}
// when the loop has checked 50 numbers it will print the amount
if ($numbers == 50){
print "There are $amount numbers with 5 divisors";
}
?>
我該如何解決? (第二是該類無效的代碼?)
您的類在while
循環中聲明,並且每次迭代都會重新聲明。
您可能的意思是,每次迭代都為其創建一個新實例。
這是修改后的代碼
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set("log_errors", 0);
// This till end of class is to get the divisor numbers
class Divisors
{
public $factor = array();
public function __construct($num) {
$this->num = $num;
}
// count number of divisors of a number
public function countDivisors() {
if ($this->num == 1) return 1;
$this->_primefactors();
$array_primes = array_count_values($this->factor);
$divisors = 1;
foreach($array_primes as $power) {
$divisors *= ++$power;
}
return $divisors;
}
// prime factors decomposer
private function _primefactors() {
$this->factor = array();
$run = true;
while($run && @$this->factor[0] != $this->num) {
$run = $this->_getFactors();
}
}
// get all factors of the number
private function _getFactors() {
if($this->num == 1) {
return ;
}
$root = ceil(sqrt($this->num)) + 1;
$i = 2;
while($i <= $root) {
if($this->num % $i == 0) {
$this->factor[] = $i;
$this->num = $this->num / $i;
return true;
}
$i++;
}
$this->factor[] = $this->num;
return false;
}
} // our class ends here
$numbers = 0;
$amout = 0;
while ($numbers < 50)
{
$numbers + 1;
$example = new Divisors($numbers);
// Here it will check if the divisor has 5 divisors
if ($example->countDivisors() == 5) {
// if true it will add 1 to the amount of numbers with 5 divisors
$amount + 1;
}
}
// when the loop has checked 50 numbers it will print the amount
if ($numbers == 50){
print "There are $amount numbers with 5 divisors";
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.