【发布时间】:2016-05-02 04:08:26
【问题描述】:
如果我的术语不正确,我们深表歉意。我想使用一个类中的方法/函数,并使用它们从另一个类文件中输出变量。这是我的主要基地:
Sphere.class.php
<?php
class SphereCalculator {
const PI = 3.14;
const FOUR_THIRDS = 1.33;
public function __construct($radius){
$this->setRadius ($radius);
}
public function setRadius ($radius){
$this->classRadius = $radius;
}
public function getRadius(){
return $this->classRadius;
}
public function getArea () {
return self::PI * ($this->classRadius * $this->classRadius);
}
}
$mySphere = new SphereCalculator ($newRadius);
所以使用这个类的函数,我想使用include从第二个php文件中输出半径和面积来拉出方法。但我真的不知道从哪里开始。我查阅了很多教程,但它们都是针对两个类的一个 php 文件。这是我所得到的。
App.class.php
<?php
include ("Sphere.class.php");
class AppClass {
function __construct($radius)
{
//something here to call $radius from
//base class and set it to $newRaduis
}
function callSphereClass ($mySphere)
{
//something to call $mySphere
//something here to allocate $newRadius
echo "The radius is ".$newRadius."<br>";
echo "The area is ".$mySphere->getArea ()."<br>";
}
}
$newRadius = 113;
$appClass = new AppClass ();
$appClass->callSphereClass();
?>
【问题讨论】:
-
注意:您可以使用
M_PI常量或pi()函数获得更准确的数字 -
或者您只是在寻找类扩展/派生?
-
我不太确定,但我认为是推导。所以它从源中提取函数,然后变量的输入和输出在第二个。
-
@Ryan Vincent 哦,这很有趣!对于我的初学者大脑来说,这也是可以理解的。很酷!
标签: php class get set php-include