Reputation: 3
Hello Fellow Stack Over Flow members, I hope you guys can help with me something that I have been unable to figure out. I have been unable to write a class for the below code, I was hoping one of you php experts could help
$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 ();
Every time I attempt to write a class it either prints out blank or Web Storm is throwing an error about method not being declared in class.
code I used:
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;
}}
any code suggestions would greatly be appreciated!
Upvotes: 0
Views: 88
Reputation: 2300
that's kind of a big mess! :) You're mixing setters/getters and direct access, and are using old school constructors. You can clean it up really quickly.
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;
}
}
If you set up your class like such with all your variables in the constructor, then you should be using it like:
$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;
Marked issues with your code were:
You're mixing members with functions. This kind of stuff in your constructor is major breakage:
$this->setName = $price
You're also mixing substring access [] with -> and using getters on top ->getName(). Be consistent, and in this case, respect the semantic of public variables.
Lastly, you had parameters in your constructor, but weren't using it at all.
Alternative would be to assign values to your constructor signature variables by default to make them optional.
public function __construct($name = "", $price = 0, $address_1 = "", $address_2 = "", $city = "", $state = "", $zip = "" )
And then use the public member access as you had:
$obj = new Property();
$obj->name = "Some Name";
Good luck with your learning.
Upvotes: 1