Jump to content
MakeWebGames

Recommended Posts

Posted

Hey there,

I finally found a few minutes of free time, and I wanted to gain more knowledge in object-orientated programming within PHP, so I hit the manual, and this is what i have created.

I have implemented the following;

 

If you have a free moment, could you see if it is OK and indicate where I could improve. Many Thanks!

About the "program"

Using the classes you input various values to each variable, and it validates it for you. The outcome using the code below is;

Harry spent £60 at Walmart
<?php
/*
Extending a class
March 29, 2011 
sniko
_val = validate class
*/

class validate
{
	/*
		This is my validation class, to check validation is ok, if not show a message
	*/
	final public function length($what, $l)
		{
			if(strlen($what) <= $l)
				{
					die($what." has to be more than ".$l." characters in length!");
				}
		}
	final public function numeric($what)
		{
			if(! is_numeric($what) )
				{
					die("You put an '".$what."' where it has to be numeric!");
				}
		}
}

class cart
{
	/*
		Declare all the variables to be used in the extended class of cart
	*/
	private $owner;
	private $price;
	private $store;
	private $_val;
}

class cart_show extends cart
{
	public function __construct()
	  {
		  /*
		  	So i can use the validation class in each individual function
		  */
	    $this->_val = new validate();
	  }
	function setOwner($name)
		{
			$this->owner = $name;
			$this->_val->length($name, 3); //validate (using the validation class) the string length
		}	
	function setPrice($price)
		{
			$this->price = $price;
			$this->_val->numeric($price); //validate (using the validation class) it is numeric
		}
	function setStore($store)
		{
			$this->store = $store;
			$this->_val->length($store, 5);
		}
}

$obj = new cart_show;

$obj->setOwner('Harry');
$obj->setPrice('60');
$obj->setStore('Walmart');

echo $obj->owner .' spent £'.$obj->price .' at '.$obj->store;
?>

Many thanks

- sniko

Posted

Ok you used a constructor and extended a class... however you didn't used it in a manner which is really sinful.

OO programming is not simply using classes and object just because you have them. Instead OO programming is there to solve issues and "groups" functions and data when those belong together.

In your case, you extended a class, but this base class is not used by any other, then why not simply have that values inside the cart_show? Or all inside cart?

The validation is also not used by others, and actually don't require objects and could work statically, so either make the function static or code the functions inside the cart as well.

Finally a logic issue, you do the check (validation) after setting the value, which is certainly wrong.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...