Problem with get() and set() method within a class

I have this code. What I want to do is echo the return value of getPrice at the bottom of the code. First I created a setPrice function with certain conditions. According to those conditions the out put should be 750, but it keeps returning 600.
Below this code, I copy a second code which outputs the correct number, but using a different logic.
Hope you can help me!

1st CODE (does not work)

class book
{
public $format = ['HARD', 'POCKET'];
public $price;



public function __construct($format,$price)
{
$this->format = $format;
$this->price = $price;

}

public function setPrice()

 {   

 if ($this->format== 'HARD')  { $this->price= $price+ $price * 1/4;
}
  else { $this->price= $price;
            }  
 }

  public function getPrice()
 {
  return $this->price;
}

}
 $book1 = new book('HARD',600);
 echo $book1->getPrice();

2nd CODE:(works). No get function. Only set with 'return' expressions.

class book
{
public $format = ['HARD', 'POCKET'];
public $price;



public function __construct($format,$price)
{
$this->format = $format;
$this->price = $price;

}

public function setPrice()
  {
$pricetotal;
if ($this->format == 'HARD') {$pricetotal = $this->price + $this->price * 1/4;
return $pricetotal;}
     else {return $this->price ;}  
  }

  $book1 = new book('HARD',600);
   echo $book1->setPrice();

the first code doesn’t use setPrice anywhere.

1 Like

setPrice should be called in the constructor as well. You want a single point of entry for everything, so when something changes, it only has to be done in one place.

1 Like

So, I have to pass setPrice() as a parameter in the constructor? Like this?

public function __construct($format,setPrice())
{
$this->format = $format;
$this->price = setPrice();

 }

No, the constructor would just call the method. Here is a proper OOP example using design patterns to look at for examples.

<?php

abstract class Book
{
	protected $_price;

	public function __construct($price)
	{
		$this->_price = setPrice($price);
	}

	public function setPrice($price)
	{   
		if ($price > 0)  { 
		 	$this->_price= $price;
		} else { 
			throw new Exception("Price cannot be less than 0");        
		}  
	}

	public function getPrice()
	{   
		return $this->_price;  
	}


}

class Hard extends Book
{
	
	public function __construct($price){
		$this->setPrice($price);
	}
	
       // we override the abstract method, because the pricing is different for this class type
	public function setPrice($price){
		$this->_price= $price+ $price * 1/4;
	}
}

class Pocket extends Book
{
	public function __construct($price){
		$this->setPrice($price);
	}
	
}
// Factory Pattern if you want to learn more.
class BookFactory
{
	
	public static function add($format, $price){
		$format = ucwords($format);
		if(class_exists($format)) {
           return new $format($price);
		} else {
			throw new Exception('Format not supported.');
		}
	}
	
}

$book1 = BookFactory::add('HARD',600);
echo "The " . get_class($book1) . " is \${$book1->getPrice()}";
echo "\r\n"; 
$book2 = BookFactory::add('pocket',600);
echo "The " . get_class($book2) . " is \${$book2->getPrice()}";
1 Like

Ok. I’m going to try it out. I’ll let you know how it goes. Thanks!

I’ve tried this. I get: 'Fatal error: Uncaught Error: Call to undefined function setPrecio() in C:\xampp\htdocs… in line $this->price = setPrice($price);
What do I need to make this code work? is it possible? or do I have to reingeer it to look somethin like you did? I want to keep it simple just to learn the concepts and how to use get and set.

 class book
 {
public $format = ['HARD', 'POCKET'];
public $price;



public function __construct($format,$price)
{
$this->format = $format;
$this->price = setPrice($price); //line of the error

}

public function setPrice($price)

 {   
 $this->price = $price;
 if ($this->format== 'HARD')  { $this->price= $price+ $price * 1/4;
}
  else { $this->price= $price;
            }  
 }

     public function getPrice()
    {
  return $this->price;
}

}
 $book1 = new book('HARD',600);
 echo $book1->getPrice();

Spanish for setPrice?

setPrecio isn’t in the code I posted or you posted, so I don’t know what’s going on with it, unless you have that word somewhere else?

Yeah. Sorry! The actual code is in spanish, I forgot to translate. It should be setPrice.

If you are still struggling, then I would do a search for the string, the link posted shows it working in it’s entirety for you to learn from.

1 Like

What string should I search , you mean ‘setPrecio’ ? What ‘link posted’ ?

whatever the string the error gives that it can’t find. So, if you changed it to setPrecio from setPrice, do a search for the method name it can’t find.

I replaced

public function __construct($format,$price)
{
$this->format = $format;
$this->price = setPrice($price); // line of the error

}

with

public function __construct($format,$price)
{
$this->format = $format;
$this-> setPrice($price); // changed this.

}

and it worked!

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service