Classes?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 2015 Views - Last Post: 15 May 2009 - 09:40 AM Rate Topic: -----

#1 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Classes?

Posted 30 April 2009 - 09:04 PM

I have added classes - and finally can get them to talk to one another but I keep getting this error:
Catchable fatal error: Object of class galleryCost could not be converted to string in /home/content/g/k/w/gkwinspired/html/store/fineArt2.php on line 163

Here is the class:

class galleryCost {
			function galleryPrice() {
				// Discount given to galleries
				$discount = ".25";
				if(!isset($_SESSION['galleryLogged']) && !isset($_COOKIE['WGGallery'])) {
				}
				else {
					$price = $price * $discount;}
			}
		}


Here is the implementation:


	while($row = mysql_fetch_array($result)){
		
		// Set the price from the db
		
			$price = $row{'price'};
			$price = new galleryCost;
			$price -> galleryPrice();
			
		// Grabs the Gallery Cost class and starts the function within the class.
		// Checks to see if the Gallery Session or Cookies are in place before changing
		// the cost of the prints.
		
	/**************************************************************************/
	// OUTPUT
	//--------------------------------------------------------------------------
		
		print "<table cellspacing=\"0\" cellpadding=\"0\">";
		
				print "<tr>";
					print "<td rowspan=\"2\" class=\"image\">";
						print "<a href=\"display.php?pageId=".$row{'id'}."\" target=\"_blank\"><img src=\"../images/gallery/".$row{'img'}."\" alt=\"".$row{'title'}."\"></a>";
					print "</td>";
				print "</tr>";
				
				print "<tr>";				
					print "<td>";
						print "<p class=\"title\">".$row{'title'}."</p>";
						print "<p class=\"price\">Price: $".$price."</p>";
						print "<p class=\"description\">Description:</p>";
						print "<p>".$row{'description'}."</p>";
						print "<p><a href=\"display.php?pageId=".$row{'id'}."\" target=\"_blank\">More Info >></a>";
					print"</td>";
					
				print "</tr>";
			print "</table>";				
				}



Please help me out - I'm stumped.

--

Greg

Is This A Good Question/Topic? 0
  • +

Replies To: Classes?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Classes?

Posted 01 May 2009 - 05:04 AM

$price = $row{'price'};
$price = new galleryCost;
$price -> galleryPrice();


Not sure what you're trying to do here. You're getting the price from the DB into the $price variable, then immediately overwriting the $price variable with a new galleryCost object, then calling the galleryPrice() function. I think what you want to do (untested) is:
class galleryCost {
    function galleryPrice($fullPrice) {
        // Discount given to galleries
        $discount = 0.25;
        if(!isset($_SESSION['galleryLogged']) && !isset($_COOKIE['WGGallery'])) {
            return $fullPrice;
        }
        else {
            return $fullPrice * (1.0 - $discount);
        }
    }
}



// Only need to create the class once, not
// every time through the loop
$cost = new galleryCost;
while($row = mysql_fetch_array($result)){
	// When you want the actual price, call your object this way:
	$actualCost = $cost->galleryPrice($row['price']);

Was This Post Helpful? 0
  • +
  • -

#3 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Classes?

Posted 01 May 2009 - 09:14 AM

That seems fine - but you never multiply the function you created - how is it getting the price, and you're never telling it to multiply the discount by the price grabbed from the database.

		   // HOW WILL IT KNOW WHAT THE PRICE IS
			return $price * (1.0 - $discount);



Is it because of this part?

$actualCost = $cost->galleryPrice($row['price']);



Will that apply the function to that element?
Was This Post Helpful? 0
  • +
  • -

#4 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 900
  • Joined: 08-August 08

Re: Classes?

Posted 01 May 2009 - 09:20 AM

your problem is that you put discount as a string and not as a number

try

$discount = 0.25;
Was This Post Helpful? 0
  • +
  • -

#5 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: Classes?

Posted 01 May 2009 - 10:02 AM

type cast it to a float, in the class.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Classes?

Posted 01 May 2009 - 10:40 AM

View Postgregwhitworth, on 1 May, 2009 - 11:14 AM, said:

That seems fine - but you never multiply the function you created - how is it getting the price, and you're never telling it to multiply the discount by the price grabbed from the database.

		   // HOW WILL IT KNOW WHAT THE PRICE IS
			return $price * (1.0 - $discount);



Is it because of this part?

$actualCost = $cost->galleryPrice($row['price']);



Will that apply the function to that element?


Yes, the way I wrote it, you pass the price from the database to the galleryPrice method of the galleryCost object, and return the true price of the item.

You could also construct the object and pass the full price to the constructor, something like this:

class priceListing
{
    public __construct($price)
    {
        $this->price = $price;
        $this->discount = 0.0;
    }

    public setDiscount($discount)
    {
        $this->discount = $discount;
    }

    public getPrice()
    {
        if ($this->requestorGetsDiscount())
            return $this->getDiscountedPrice();
        else
            return $this->price;
    }

    private getDiscountedPrice()
    {
        return $this->price * (1.0 * $this->discount);
    }

    private requestorGetsDiscount()
    {
        // You might want to add other reasons for a discount, like it's Tuesday
        return ($this->requestorIsAGallery() || $this->todayIsTuesday());
    }

    private requestorIsAGallery()
    {
        return (!isset($_SESSION['galleryLogged']) && !isset($_COOKIE['WGGallery']));
    }

    private todayIsTuesday()
    {
        return (getdate()['wday'] == 2);
    }
}


and call it like this:

while($row = mysql_fetch_array($result)){
    $cost = new priceListing($row['price']);
    $cost->setDiscount(0.25);
    echo 'Price: $' . $cost->getPrice() . "\n"
}


Was This Post Helpful? 0
  • +
  • -

#7 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Classes?

Posted 01 May 2009 - 01:54 PM

I'm intrigued, very intrigued really. When you did the following:
 public setDiscount($discount)
	{
		$this->discount = $discount;
	}



It baffled me - I was like, "What does discount equal. Then I saw this:

 $cost->setDiscount(0.25);



Does this mean that by saying setDiscount($discount) you are saying that anything that is in the parenthesis when the function is called is set as the discount variable?

--

Greg
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Classes?

Posted 01 May 2009 - 06:18 PM

Yes, but there were some errors in there. Here's a full, tested command line program to sort of demonstrate:
<?php

class priceListing
{
    // Creates two variables associated with
    // a priceListing: the price, and a discount
    // Because they are private, they are not accessible
    // outside of the class; i.e., try to get them like
    // this will not work:
    // $cost = new priceListing(400);
    // print $cost->price;
    
    private $price;
    private $discount;
    
    public function __construct($price)
    {
        // We're passed a parameter $price when
        // creating a priceListing object, which
        // should contain the base price this listing
        // should have. 
        // Set *the object's* $price variable to what
        // is in the $price argument; notice how we have
        // to use $this-> to access the object's price.
        $this->price = $price;
        
        // Give the discount variable a default of 0.
        $this->discount = 0.0;
    }

    public function setDiscount($discount)
    {
        // Like in the constructor, we're passed a 
        // desired discount in the $discount argument,
        // so set the object's $discount variable to
        // the passed-in value;
        if ($discount < 1.0)
            $this->discount = $discount;
        else
            $this->discount = $discount / 100;
    }

    public function getDiscount()
    {
        // Just return the current discount as a percentage
        return $this->discount * 100;
    }
    
    public function getPrice()
    {
        // Check to see if the requestor is 
        // qualified in some way for a discount. 
        // If so, calculate the discounted price 
        // and return it to the caller. Otherwise,
        // just return the base price.
        if ($this->requestorGetsDiscount())
        {
            return $this->getDiscountedPrice();
        }
        else
        {
            return $this->price;
        }
    }

    // Notice I've made this private, meaning it
    // can only be accessed from within the class.
    private function getDiscountedPrice()
    {
        return $this->price * (1.0 - $this->discount);
    }

    private function requestorGetsDiscount()
    {
        // You might want to add other reasons 
        // for a discount, like it's Tuesday
        return ($this->requestorIsAGallery() || $this->todayIsTuesday());
    }

    private function requestorIsAGallery()
    {
        return (!isset($_SESSION['galleryLogged']) && !isset($_COOKIE['WGGallery']));
    }

    private function todayIsTuesday()
    {
        $today = getdate(); 
        return ($today['wday'] == 2);
    }
}

$piecePrice = 400;
$nonGallery = new priceListing($piecePrice);
echo ("Your non-gallery discount is " . $nonGallery->getDiscount() . "%\n");
echo "Your non-gallery price is $" . $nonGallery->getPrice() . "\n";

$gallery = new priceListing($piecePrice);
$gallery->setDiscount(0.25); // 25%
echo "Your gallery discount is " . $gallery->getDiscount() . "%\n";
echo "Your gallery price is $" . $gallery->getPrice() . "\n";

$tateGallery = new priceListing($piecePrice);
$tateGallery->setDiscount(50); // 50%...it's the Tate, after all!
echo "Your Tate Gallery discount is " . $tateGallery->getDiscount() . "%\n";
echo "Your Tate Gallery price is $" . $tateGallery->getPrice() . "\n";

?>

Was This Post Helpful? 1
  • +
  • -

#9 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Classes?

Posted 05 May 2009 - 01:28 PM

I want to be able to send multiple variables to an image upload class that I am creating. Can you give me some tips, I've tried the normal way - but even though I've called the class they don't exist. How do I send more than one to the class:

class fileUpload {
			function checkMime($file) {
				
				$mime = $_FILES['file']['type'];
				$origName = strrchr($_FILES['file']['name'], '.');
				$completeName = $uploaddir . basename($_FILES['file']['name']);
				
				if (!in_array($mime, $mime_types)) {
					$posterrs['wrongMime'] = "The file that you uploaded is not supported by Koniag Education.";
					return $posterrs;
					}
				else {
					return $mime;}}
				
			function upload($file) {
			
			// Checks the original file to make sure that it truly exists.
				if(!isset($_FILES['file']) && empty($_FILES['file'])) {
					$posterrs['noFile'] = "You have to upload a file.";
					return $posterrs;
					}
				else {
					$file = $_FILES['file'];
					return $file;}
					
			
			}
		}



Don't bash the code yet - it is a work in progress.

Thanks.

--

Greg
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: Classes?

Posted 05 May 2009 - 01:39 PM

$x = new fileUpload;
$x->a = "Value for variable a of the object: x";
$x->b = "Value for variable b of the object: x";
$x->c = "Value for variable c of the object: x";

Was This Post Helpful? 1
  • +
  • -

#11 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Classes?

Posted 13 May 2009 - 11:16 AM

About this though. For instance - I want to get the temporary file's name and don't want to clutter up my code with this as I will do this every time I upload a file. Am I able to call the file name from within the class - I can't seem to get that function to work though.
Was This Post Helpful? 0
  • +
  • -

#12 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Classes?

Posted 13 May 2009 - 01:32 PM

Here's a class I'm building that will handle file uploads for pretty much any file, let me know if you see how it won't work:

class file_upload {
					function upload($uploaddir) {
						$mime = $_FILES['file']['type'];
						$filename = $_FILES['file']['name'];
						$filesize_ini = $_FILES['file']['size'];
						$filesize_total = $filesize_ini/1024;
						
						echo $mime;
						echo $filename;
						echo $filesize_ini;
						
						exit;
						
						 // strip extention from filename
						$file_basename = substr($filename, 0, strripos($filename, '.'));
						$file_ext = substr($filename, strripos($filename, '.'));
						
						// Final file name
						$fileComplete = $filename . $file_ext;
						$uploaddir = $uploaddir . $fileComplete;
						
						//
						//  MIME TYPES
						//
						//******************************
						
							$mime_app = array("application/pdf", "application/msword","text/plain", "application/zip", "application/rtf", "application/ms-excel", "application/octet-stream", "application/vnd.ms-excel");
							
							$mime_img = array("image/gif","image/jpeg","image/tiff","image/png");
							
							$mime_audio = array("audio/mpeg","audio/midi","audio/x-wav","audio/x-aiff");
							
							$mime_text = array("text/css","text/html","text/plain","text/richtext","text/rtf","text/xml");
							
							$mime_video = array("video/flv","video/mpeg","video/mp4","video/quicktime","video/x-ms-wmv","video/x-ms-wmx","video/x-msvideo");
							
							$mime_all = array("application/pdf", "application/msword","text/plain", "application/zip", "application/rtf", "application/ms-excel", "application/octet-stream", "application/vnd.ms-excel","image/gif","image/jpeg","image/tiff","image/png","audio/mpeg","audio/midi","audio/x-wav","audio/x-aiff","text/css","text/html","text/plain","text/richtext","text/rtf","text/xml","video/flv","video/mpeg","video/mp4","video/quicktime","video/x-ms-wmv","video/x-ms-wmx","video/x-msvideo");
							
							
						//
						//  TESTING OF FILE
						//
						//******************************
							
								if (!in_array($mime, $mime_types)) {
									$posterrs['wrongMime'] = "The file that you uploaded is not supported by Koniag Education.";
								}	
								
								if(!isset($_FILES['file']) && empty($_FILES['file']) || $_FILES == "") {
									$posterrs['noFile'] = "You have to upload a file.";
								}
									
								if($filesize > $filesize_total) {
									$posterrs['filesize'] = "Your file is too large, please upload a smaller file.";
								}
								
								if(!move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir)) {	
									$posterrs['uploadError'] = "There was an error uploading your file, please try again.";
								}	
					}
						return $posterrs;
						return $fileComplete;						
				}


Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: Classes?

Posted 13 May 2009 - 01:36 PM

I haven't tried it, but I noticed that your class consists of one function. If you don't plan on adding more, you could just use the function and not need to create an object.
Was This Post Helpful? 0
  • +
  • -

#14 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Classes?

Posted 13 May 2009 - 01:46 PM

Ok so - what I should do - is combine my Post Cleaner function and my file upload function into one class - got it.

Do you see anything else within that class - that you would do something different?

And I just tested it - and yes you can receive information from the $_FILES array from within a class.
Was This Post Helpful? 0
  • +
  • -

#15 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Classes?

Posted 13 May 2009 - 01:51 PM

Ok. Another issue. I'm telling what the mime type I want on upload.php, and sending that variable(per your instructions) to modules.php to the upload object where I am trying to call it to use it in the function but I can't seem to get it to work.

Upload.php
// IMAGE SETTINGS
$uploaddir = 'uploads_2009/';
$filename = $fName . $lName . $year;
$mime_types = $mime_app; // You can use $mime_app, $mime_img, $mime_audio, $mime_text, $mime_video, $mime_all 
$completePath = "http://www.koniageducation.org/sb/"; // Don't include filename or upload directory
$filesize = 3600;

$fileUpload = new file_upload;
$fileUpload->mime_types = $mime_types;
$fileUpload->upload($uploaddir);



Modules.php - Upload Object
class file_upload {
					function upload($uploaddir) {
						$mime = $_FILES['file']['type'];
						$filename = $_FILES['file']['name'];
						$filesize_ini = $_FILES['file']['size'];
						$filesize_total = $filesize_ini/1024;
						$mime_types = $mime_types;
						
						echo $mime;
						echo $filename;
						echo $filesize_ini;
						echo $mime_types;
						exit;



Help please.

--

Greg
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3