Jump to content

PHP: Class, Objects, Methods.


jacob1986

Recommended Posts

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";

 

?>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

I have rewrote the code... but this time it only prints: Author: Willa

 

It should read: Author Willa Cather

 

 

<?php
class 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";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.