jacob1986 Posted December 5, 2015 Share Posted December 5, 2015 I have typed some PHP code from a book (https://books.google.co.uk/books?id=KZoAq_mbhXAC&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=false) page 20. But keep getting an error message? Parse error: syntax error, unexpected 'public' (T_PUBLIC) PHP Code: <?php class MyClass { //class body}public function myMethod($argument, $another) { //...}$myObj = new MyClass();$myObj -> myMethod( "Harry Potter");// //let's declare a method in our ShopProduct class:class ShopProduct { public $title = "default product"; public $producerMainName = "main name"; public $producerFirstName = "first name"; public $price = 0; function getProducer() { return "{$this -> producerFirstName}" . "{$this -> producerMainName}"; }}$product1 = new ShopProduct();$product1 -> title = "My Antonia";$product1 -> producerMainName = "Cather";$product1 -> producerFirstName = "Willa";$product1 -> price = 5.99;print "Author: {product1 -> getProducer()}\n"; ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted December 5, 2015 Share Posted December 5, 2015 myMethod is supposed to be inside the class body, like with getProducer and ShopProduct. Buuut I'm pretty sure that whole class is just supposed to be a visual example. Not actual code you use. Quote Link to comment Share on other sites More sharing options...
jacob1986 Posted December 5, 2015 Author Share Posted December 5, 2015 Regarding the code it [the book] does say 'this outputs the following: author: Wills Cather' Could you or maybe someone else help me to sort the code so it prints: author: Wills Cather As below? Or have done it wrong? <?php class MyClass { $myObj = new MyClass(); $myObj -> myMethod( "Harry Potter");}public function myMethod($argument, $another) { //...} // //let's declare a method in our ShopProduct class:class ShopProduct { public $title = "default product"; public $producerMainName = "main name"; public $producerFirstName = "first name"; public $price = 0; Quote Link to comment Share on other sites More sharing options...
jacob1986 Posted December 5, 2015 Author Share Posted December 5, 2015 I have rewrote the code... but this time it only prints: Author: Willa It should read: Author Willa Cather <?phpclass ShopProduct { public $title; public $producerMainName; public $producerFirstName; public $price = 0; function __construct ( $title, $firstName, $mainName, $price) { $this-> title = $title; $this-> producerFirstName = $firstName; $this-> produerMainName = $mainName; $this -> price = $price; } function getProducer() { return "{$this -> producerFirstName}". "{$this -> producerMainName}"; }}$product1 = new ShopProduct( "My Antonia", "Willa", "Cather", 5.99);print "Author: {$product1->getProducer()}\n";?> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 5, 2015 Share Posted December 5, 2015 $this-> produerMainName = $mainName; You need to get a proper IDE. We can't play the human debugger forever. Quote Link to comment Share on other sites More sharing options...
jacob1986 Posted December 5, 2015 Author Share Posted December 5, 2015 I'm really sorry guys!!! I fervently apologise! Quote Link to comment 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.