Skip to main content
Participant
January 12, 2021
Answered

DV 2021 CC, public function conversion($country){ ... syntax error - unexpected public (T_PUBLIC)

  • January 12, 2021
  • 1 reply
  • 265 views

Hello, everyone,

line nr 13 is red and when hover over it it shows syntax error, unexpected public (T_PUBLIC)

13    public function conversion($country){

I belive PHP is version 7.1 by default.

What should I do?

Thanks,

Dragan, Toronto, Canada

    This topic has been closed for replies.
    Correct answer osgood_

    It looks to me like you have a function within a function. Shouldn't it be:

     

    $this->rentalPrice = $pRentalPrice;

    } // Close 'construct' function (currently the closing bracket is misssing from your code)

     

    //Open conversion function

    public function conversion($country) {

     

    // function code goes here

     

    }

    // Close 'conversion' function

    }

    // Close 'Movie' class

    }

    // Currently you have the extra closing bracket here which I think should be closing the 'construct' function

     

     

    This is all a maybe - I dont write this kind of advanced php code so I could be completely incorrect.

    1 reply

    BenPleysier
    Community Expert
    Community Expert
    January 12, 2021

    I am sorry, but I cannot see what you see. Where and when do you see the error message, is it in Dreamweaver, is it in the browser, is it the local version or the remote version. Is the server PHP v.7.1 or is it just the code. Where did you get the code from? What does the code look like?

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    Participant
    January 12, 2021

    Hi, Ben,

    Thanks for the replay. This is the situation:

    I have DV open in split view. It is not that error message appears if I run the code. No. it is a PHP file and the code I copied from a book. The code has 29 lines with numbers if front of each line. All numbers are gray (normal) except number 13 is red. If I hover over that red number 13 then the message above it appears: "syntax error, unexpected 'public' (T_PUBLIC)".

    This is the line 13:

    public function conversion($country){

    I just started learning (my first lesson) OOP in PHP and this line is supposed to define a public function inside a class description. The example is from a book from SitePoint (Learn PHP in One Day and Learn It Well, chapter 9.2 - Writing our own class).

    Bellow is the code . The line in question is in bold (I added most of the comments myself for a reference):

    <?php
    class Movie{
        //Add class members here -  properties (variables), constants and methds (functions):
        private $id; //property - just DECLARED for now, not initialized yet. It will be initialized later by a special method called constructor.
        public $title; //property - just DECLARED for now, not initialized yet. It will be initialized later by a special method called constructor.
        public $rentalPrice; //property - just DECLARED for now, not initialized yet. It will be initialized later by a special method called constructor.
        const DISCOUNT = 10; //constant - declared AND assigned a value. NOTE: To define CONSTANT within a class we use constant KEYWORD, instead define() FUNCTION.
        /* There are some methods (functions)in PHP that have special functionalities and thery are called MAGIC METHODS. Their names are predefined and start with __ The first one to be called whenever we create an object from the class is __construct() and is named CONSTRUCTOR. It is typically used to initialize properties inside the class.*/
        public function __construct($pId, $pTitle, $pRentalPrice){// Here, we define a constructor with three parameters.
        $this->id = $pId; // property - initialized just now. Whenever we want to access the properties and methods of a class inside the class, we use the $this keyword, followed by the -> operator.
        $this->title = $pTitle; // property - initialized just now. Whenever we want to access the properties and methods of a class inside the class, we use the $this keyword, followed by the -> operator.
        $this->rentalPrice = $pRentalPrice; /* property - initialized just now. Whenever we want to access the properties and methods of a class inside the class, we use the $this keyword, followed by the -> operator.*/
        public function conversion($country){
        $rate = 1;
        switch($country){ // this is a parameter so we don't use keyword $this.
            case 'UK':
              $rate = 0.76; // this is a local variable (variable declared inside the method) so we don't use keyword $this.
               break;
             case 'Japan':
               $rate = 110; // this is a local variable (variable declared inside the method) so we don't use keyword $this.
               break;
        }
     
        return round($rate*$this->rentalPrice, 2);
    }
    }
       
    }

    ======// end of code//=====

    Nothing serious or important, just practicing, but would like to learn an understand properly and in details.

    Thank you for your time and understanding,

    Dragan

     

    osgood_Correct answer
    Legend
    January 12, 2021

    It looks to me like you have a function within a function. Shouldn't it be:

     

    $this->rentalPrice = $pRentalPrice;

    } // Close 'construct' function (currently the closing bracket is misssing from your code)

     

    //Open conversion function

    public function conversion($country) {

     

    // function code goes here

     

    }

    // Close 'conversion' function

    }

    // Close 'Movie' class

    }

    // Currently you have the extra closing bracket here which I think should be closing the 'construct' function

     

     

    This is all a maybe - I dont write this kind of advanced php code so I could be completely incorrect.