The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP5 - Valid Class?
Discuss Valid Class? in the PHP Development forum on Dev Shed. Valid Class? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 4th, 2012, 11:11 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
|
|
|
PHP5 - Valid Class?
Does this look like a valid class? I mean if it were to be a part of a program, does it look ok?
class pSelector {
Public $pType;
$error = 'Please input a price.';
if(this->price > 2000) {
$pType = 'House';
echo(this->$pType);
}
else if(this->price < 2001) {
$pType = 'Apartment';
echo(this->$pType);
}
else {
echo($error);
}
}
|

December 4th, 2012, 11:41 PM
|
 |
Lost in code
|
|
|
|
|
No, you can't have code in the middle of a class, it needs to be inside a function.
|

December 5th, 2012, 12:15 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
|
|
|
Thanks, this better?
class pSelector {
Public $pType;
$error = 'Please input a price.';
function $pselect(){
if(this->price > 2000) {
$pType = 'House';
echo(this->$pType);
}
else if(this->price < 2001) {
$pType = 'Apartment';
echo(this->$pType);
}
else {
echo($error);
}
}
$pselect();
}
|

December 5th, 2012, 12:43 AM
|
 |
Code Monkey V. 0.9
|
|
Join Date: Mar 2005
Location: A Land Down Under
|
|
Closer but still wrong. - Function names do not use a $ in front, only variables.
- this->price should have a $ in front - $this->price
- $this->price actually needs to be declared. It may work depending on your error settings.
- All internal class variables must be referenced by $this-> so you can't use $error as you have there, it should be $this->error
- Again... You can't have code outside of functions, so your call to $pselect() is not valid (and remember to remove the $ in front of the function name).
- Also, internal function names are referenced using $this-> as well, so it would be $this->pselect();
That's all that I can see for now.
Oh, and you should use PHP tags to wrap your code in when you paste in on here. That way it will keep the indenting and make it a lot more readable.
|

December 5th, 2012, 12:53 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
|
|
Thanks, ok - the class is is set to figure the type of rental it is likely to be based on price. $obj->price = '2000'; is set earlier on in the code. I appreciate the help.
PHP Code:
class pSelector {
Public $pType;
$error = 'Please input a price.';
function pselect(){
if($this->price > 2000) {
$pType = 'House';
echo($this->$pType);
}
else if($this->price < 2001) {
$pType = 'Apartment';
echo($this->$pType);
}
else {
echo($error);
}
}
}
$this-> pselect();
|

December 5th, 2012, 01:03 AM
|
 |
Code Monkey V. 0.9
|
|
Join Date: Mar 2005
Location: A Land Down Under
|
|
Ok, what you've still missed: - You never actually create an object so you can't call any of it's methods.
- You're still using $error when it should be $this->error
- You're still using $this->$ptype when it should be $this->ptype
- You never set up $price anywhere.
e - elseif() should be a single word, no spaces. It still works as else if() but that's not the current way to do it.
Are you working on an editor with syntax highlighting and code "inteligence"? I don't believe so because if you were 99% of the problems that you're going through now would be shown straight away. I'd suggest looking at an editor that can do this for you. My editor of choice is Komodo Edit (the free version) and it does all of this for you.
As a valid example of what you are looking for:
PHP Code:
class pSelector {
public $pType;
public $error = 'Please input a price.';
public $price = 0;
function pselect(){
if($this->price > 2000) {
$this->pType = 'House';
echo($this->pType);
}
elseif($this->price < 2001) {
$pType = 'Apartment';
echo($this->pType);
}
else {
echo($this->error);
}
}
}
$p_select = new pSelector ();
$p_select->pselect ();
|

December 5th, 2012, 01:33 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi DAR1,
I think it might be a good idea to take some time and actually learn the syntax and logic of object orientation in PHP.
I mean, sure, you can probably put together some classes through trial and error and help from others. But you'll have much better results and be much faster if you actually know what you're doing.
There's even an OOP tutorial at php.net.
|

December 5th, 2012, 01:56 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
|
|
Do I need to define $price still?
PHP Code:
include('Property.php');
$obj = new Property ();
$obj->setName ('My Property');
$obj->price = 2500.00;
$obj ['address_primary'] = '100 Main St';
$obj->address_secondary = 'STE 100';
$obj->city = 'Townsville';
$obj->state = 'VA';
$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 ();
class pSelector {
public $pType;
public $error = 'Please input a price.';
public $price = 0;
function pselect(){
if($this->price > 2000) {
$this->pType = 'House';
echo($this->pType);
}
elseif($this->price < 2001) {
$pType = 'Apartment';
echo($this->pType);
}
else {
echo($this->error);
}
}
}
$p_select = new pSelector ();
$p_select->pselect ();
|

December 5th, 2012, 02:08 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
|
|
|
Trust me Jacquez, in 5 min at this forum I've learned more than if I were to read a long *** tutorial. I learn differently.
|

December 5th, 2012, 02:19 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Fair enough, there are some type of people who learn better that way. But are you learning or are you following directions? Do you understand what not just what they're telling you but why things are the way they are?
|

December 5th, 2012, 02:25 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by requinix Fair enough, there are some type of people who learn better that way. But are you learning or are you following directions? Do you understand what not just what they're telling you but why things are the way they are? |
Yes, I learned that the object identifier $this-> should be used to identify which object we are utilizing. I learned that classes can utilize functions and are not to be mistaken with functions. I learned that else if and elseif are used differently.
I am still curious about whether I need to define $price since is referenced as $obj->price and set. I do not want to change the price.
|

December 5th, 2012, 04:31 PM
|
 |
Code Monkey V. 0.9
|
|
Join Date: Mar 2005
Location: A Land Down Under
|
|
Quote: | Originally Posted by DAR1 I am still curious about whether I need to define $price since is referenced as $obj->price and set. I do not want to change the price. |
In the example that you gave above, $price is set in the Property object type, so this means that the pSelect object doesn't need $price to be set.
BUT... How does the pSelect object know what the price is when it doesn't have any reference to the Property object where the price is actually set? You need to pas in the Property object to the pSelect object somewhere so that it knows what it's supposed to be looking at. As a suggestion...
PHP Code:
$p_select->pselect ($obj);
Then in the pSelect object...
PHP Code:
class pSelector {
public $pType;
public $error = 'Please input a price.';
public $price = 0;
function pselect($property){
if($property->price > 2000) {
$this->pType = 'House';
echo($this->pType);
}
elseif($property->price < 2001) {
$pType = 'Apartment';
echo($this->pType);
}
else {
echo($this->error);
}
}
}
|

December 6th, 2012, 12:17 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 11
Time spent in forums: 1 h 45 m 16 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Catacaustic In the example that you gave above, $price is set in the Property object type, so this means that the pSelect object doesn't need $price to be set.
BUT... How does the pSelect object know what the price is when it doesn't have any reference to the Property object where the price is actually set? You need to pas in the Property object to the pSelect object somewhere so that it knows what it's supposed to be looking at. As a suggestion...
PHP Code:
$p_select->pselect ($obj);
Then in the pSelect object...
PHP Code:
class pSelector {
public $pType;
public $error = 'Please input a price.';
public $price = 0;
function pselect($property){
if($property->price > 2000) {
$this->pType = 'House';
echo($this->pType);
}
elseif($property->price < 2001) {
$pType = 'Apartment';
echo($this->pType);
}
else {
echo($this->error);
}
}
}
|
Thanks, I appreciate the help. I understand now. I redid the class here, if you don't mind taking a look and letting me know what you think. I am curious to know what you think about the way I structured the class and constructor.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|