Kell Posted March 22, 2021 Share Posted March 22, 2021 Hi I am new to PHP and I need to create an array and add both books to the array and than loop through the array this is where I am having trouble if the price of a book is less than $20.00 output the book’s name and price if the book’s title comes after the letter R in the alphabet output the book’s title. I am getting these error: Notice: Trying to get property 'price' of non-object in C:\xampp\htdocs\PHP objects\books.php on line 45Notice: Trying to get property 'title' of non-object in C:\xampp\htdocs\PHP objects\books.php on line 49Notice: Trying to get property 'price' of non-object in C:\xampp\htdocs\PHP objects\books.php on line 50Fatal error: Uncaught Error: Call to a member function get_first_char() on string in C:\xampp\htdocs\PHP objects\books.php:52 Stack trace: #0 {main} thrown in C:\xampp\htdocs\PHP objects\books.php on line 52 My code is: <?php class Book { // Properties public $price; public $title; public function get_first_char() { $titleArray = str_split($this->title); return reset($titleArray); } // Methods function set_price($price) { $this->price = $price; } function get_price() { return $this->price; } function set_title($title) { $this->title = $title; } function get_title() { return $this->title; } } $first_book = new Book(); $first_book->set_price('$13.95'); $first_book->set_title('Moby Dick'); echo "Price: " . $first_book->get_price(); echo "<br>"; echo "Title: " . $first_book->get_title(); echo "<br>"; $second_book = new Book(); $second_book->set_price('$23.99'); $second_book->set_title('Wuthering Heights'); echo "Price: " . $second_book->get_price(); echo "<br>"; echo "Title: " . $second_book->get_title(); echo "<br>"; $book = array("Moby Dick", "Wuthering Heights"); echo "Book Titles: " . $book[0] . ", " . " and " . $book[1] . "."; foreach ($book as $bookItem) { $price = $bookItem->price; // Right now $price is a string ('$xx.xx'), we need it to be numeric $price = (float) substr($price, 1); if ($price < 20) { echo $bookItem->title; echo $bookItem->price; } if (ord(strtoupper($bookItem->get_first_char())) > ord('R')) { echo $bookItem->title; } } ?> Any help will be very much appreciated Thank you Quote Link to comment https://forums.phpfreaks.com/topic/312350-php-objects/ Share on other sites More sharing options...
maxxd Posted March 22, 2021 Share Posted March 22, 2021 37 minutes ago, Kell said: $book = array("Moby Dick", "Wuthering Heights"); What you've done is create an array of strings. The Book objects that represent 'Moby Dick' and 'Wuthering Heights' are $first_book and $second_book. So this is what you're looking for: $book = array($first_book, $second_book); 1 Quote Link to comment https://forums.phpfreaks.com/topic/312350-php-objects/#findComment-1585267 Share on other sites More sharing options...
Kell Posted March 22, 2021 Author Share Posted March 22, 2021 Thank you so much I have it working now Quote Link to comment https://forums.phpfreaks.com/topic/312350-php-objects/#findComment-1585269 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.