0

你好 Stack Over Flow 成员,我希望你们能帮助我一些我无法弄清楚的事情。我一直无法为下面的代码编写一个类,我希望你们中的一位 php 专家能提供帮助

$obj = new ClassName ();
$obj->setName ('Name of Something');
$obj->price = 500.00;
$obj ['address_primary'] = 'First Line of Address';
$obj->address_secondary = 'Second Line of Address';
$obj->city = 'the city';
$obj->state = 'ST';
$obj->setZip (12345);

echo 'Name :: ', $obj->name, PHP_EOL;
echo 'Price :: $', $obj ['price'], PHP_EOL;
echo 'Address :: ', $obj->address_primary, ' ', $obj->getAddressSecondary (), PHP_EOL;
echo 'City, State, Zip :: ', $obj->city, ', ', $obj ['state'], ' ', $obj->getZip ();

每次我尝试编写一个类时,它要么打印出空白,要么 Web Storm 抛出一个关于方法未在类中声明的错误。

我使用的代码:

var $name;         // House Name
var $price;   // Price of House
var $address_1;         // Address 1
var $address_2;    // Address 2
var $city; // City
var $state; // state
var $zip; // zip


// Class Constructor
function Property($name, $price, $address_1, $address_2, $city, $state, $zip) {
$this->setName = $name;
$this->price = $price;
$this->address_primary = $address_1;
$this->address_secondary = $address_2;
$this->city = $city;
$this->state = $state;
$this->zip = $zip;
}
// Getter/Setter functions
function get_name() {
return $this->name;
}
function set_name($newname) {
$this->name = $newname;
}
function get_price() {
return $this->price;
}
function set_price($newprice) {
$this->price = $newprice;
}
function get_address_1() {
return $this->address_primary;
}
function set_address_1($newaddress_1) {
$this->address_primary = $newaddress_1;
}
function get_address_2() {
return $this->address_secondary;
}
function set_address_2($newaddress_2) {
$this->address_secondary = $newaddress_2;
}
function get_city() {
return $this->city;
}
function set_city($newcity) {
$this->city = $newcity;
}
function get_state() {
return $this->state;
}
function set_state($newstate) {
$this->state = $newstate;
}
function get_zip() {
return $this->setZip;
}
function set_zip($newzip) {
$this->setZip = $newzip;
}}

任何代码建议将不胜感激!

4

1 回答 1

1

这真是一团糟!:) 您正在混合 setter/getter 和直接访问,并且正在使用老式的构造函数。你可以很快清理它。

class Property
{
    public function __construct($name, $price, $address_1, $address_2, $city, $state, $zip) 
    {
            $this->name = $name;
            $this->price = $price;
            $this->address_primary = $address_1;
            $this->address_secondary = $address_2;
            $this->city = $city;
            $this->state = $state;
            $this->zip = $zip;
    }
}

如果您在构造函数中使用所有变量设置类,那么您应该像这样使用它:

$obj = new Property(
    "Name of Something",
    500,
    "First Line",
    "Second Line",
    "City",
    "State",
    "90210"        
);


echo 'Name :: ', $obj->name, PHP_EOL;
echo 'Price :: $', $obj->price, PHP_EOL;
echo 'Address :: ', $obj->address_primary, ' ', $obj->address_secondary, PHP_EOL;
echo 'City, State, Zip :: ', $obj->city, ', ', $obj->state, ' ', $obj->zip;

您的代码的标记问题是:

您将成员与功能混合在一起。您的构造函数中的这种东西是重大破坏:

$this->setName = $price

您还将子字符串访问 [] 与 -> 混合,并在顶部使用 getter ->getName()。保持一致,在这种情况下,尊重公共变量的语义。

最后,您的构造函数中有参数,但根本没有使用它。

替代方法是默认为您的构造函数签名变量赋值,以使它们成为可选的。

public function __construct($name = "", $price = 0, $address_1 = "", $address_2 = "", $city = "", $state = "", $zip = "" )

然后像以前一样使用公共成员访问权限:

$obj = new Property();
$obj->name = "Some Name"; 

祝你学习顺利。

于 2013-02-05T20:52:49.533 回答