Problem when printing data from a POO php constructor [closed]

3

I'm studying POO in php, and I've done this code, but when I run it, the page goes blank and does not show me the data I've entered before, any help?

<?php 

class coche{
    var $ruedas;
    var $color;
    var $motor;
}
function __construct(){
    $this->ruedas=4;
    $this->color="";
    $this->motor=1600;
}
function arrancar(){
    echo "Estoy arrancando";
}
function frenar (){
    echo "Estoy frenando";
}
function girar(){
    echo "Estoy girando";
}

$renault=new coche();
$chevrolet= new coche();
$mazda = new coche();

$mazda->girar();
echo $mazda->ruedas;
 ?>

and I get this error

  

Fatal error: Call to undefined method car :: rotate () in   C: \ wamp \ www \ Php Course \ Video 14. (Programming Oriented to   Objects) \ POO (III) (Calls to methods with parameters and reuse   of code) \ poo3.php on line 36

Line 36 would be $ mazda- > rotate (); I do not see the error

    
asked by Victor Escalona 26.03.2018 в 07:05
source

1 answer

3

First of all you are using the reserved word var , not bad, but it is PHP 4 version . The methods you are declaring outside. Your class I would write it like this:

class coche
{
    public $ruedas;
    public $color;
    public $motor;

    function __construct()
    {
        $this->ruedas=4;
        $this->color="";
        $this->motor=1600;
    }
    function arrancar()
    {
        echo "Estoy arrancando";
    }
    function frenar()
    {
        echo "Estoy frenando";
    }
    function girar()
    {
        echo "Estoy girando";
    }
}

Although it is always advisable to use private variables and access them by a method, as a matter of good practice.

    
answered by 26.03.2018 / 07:27
source