php builder

3

I am trying to create a class with a constructor but it is returning me this error. I tried to do something similar to Java since I do not program in PHP yet, but it is returning this error:

  

Notice: Undefined variable: typegraph in   C: \ inetpub \ wwwroot \ HOPE \ php \ graphics.php on line 23

I have tried to use the methods GETTERS and SETTERS and even then I could not solve it.

class Grafico{

    private $typegraph=0;
    private $graphTitle=0;
    private $HorizontalTitle=0;
    private $sufixo=0;
    private $lineName=0; 
    private $valuesdescription=0;
    private $values=0;

    public function Grafico($typegraph_p, $graphTitle_p, $HorizontalTitle_p, $sufixo_p, $lineName_p, $valuesdescription_p, $values_p){
        $typegraph = $typegraph_p;
        $graphTitle = $graphTitle_p;
        $HorizontalTitle = $HorizontalTitle_p;
        $sufixo = $sufixo_p;
        $lineName = $lineName_p;
        $valuesdescription = $valuesdescription_p;
        $values = $values_p;
    }

    public function Printa(){

        echo $typegraph;
        echo $graphTitle;
        echo $HorizontalTitle;
        echo $sufixo;
        echo $lineName; 
        echo $valuesdescription;
        echo $values;
    }

}
    
asked by anonymous 20.02.2017 / 14:23

2 answers

8

The variables in which you are giving echo are part of the class, so they need this to be accessed. In the way it is, PHP will look for the variables first in the scope of the function / method and as they are not declared the error occurs.

public function Printa(){
    echo $this->typegraph;
    echo $this->graphTitle;
    echo $this->HorizontalTitle;
    echo $this->sufixo;
    echo $this->lineName; 
    echo $this->valuesdescription;
    echo $this->values;
}

In this way you are referencing the class variable, this middle that points to the class itself, it is actually the class instance.

The same thing goes for the Grafico method

public function Grafico($typegraph_p, $graphTitle_p, $HorizontalTitle_p, $sufixo_p, $lineName_p, $valuesdescription_p, $values_p){
    $this->typegraph = $typegraph_p;
    $this->graphTitle = $graphTitle_p;
    $this->HorizontalTitle = $HorizontalTitle_p;
    $this->sufixo = $sufixo_p;
    $this->lineName = $lineName_p;
    $this->valuesdescription = $valuesdescription_p;
    $this->values = $values_p;
}
Just reinforcing, what you were doing was creating variables within the method while the properties of the class remained "untouched."

Read the properties session in the PHP manual, it will help a lot to understand the concept.

    
20.02.2017 / 14:27
3

Your problem is being caused due to escopo of variables. When referencing / accessing a method of the class in question, use the $this-> operator.

Another detail is that you are attempting to start the constructor of the class with a method of the same name, such as JAVA , and declared to be obsolete from the 5.3.3 version and becoming a common method when you should use the __construct method.

<?php

public class Grafico {

    private $typegraph;
    private $graphTitle;
    private $HorizontalTitle;
    private $sufixo;
    private $lineName; 
    private $valuesdescription;
    private $values;

    function __construct(
        $typegraph, 
        $graphTitle, 
        $HorizontalTitle, 
        $sufixo, 
        $lineName, 
        $valuesdescription, 
        $values
    ){
        $this->typegraph = $typegraph;
        $this->graphTitle = $graphTitle;
        $this->HorizontalTitle = $HorizontalTitle;
        $this->sufixo = $sufixo;
        $this->lineName = $lineName;
        $this->valuesdescription = $valuesdescription;
        $this->values = $values;
    }

    public function printa()
    {
        echo $this->typegraph;
        echo $this->graphTitle;
        echo $this->HorizontalTitle;
        echo $this->sufixo;
        echo $this->lineName; 
        echo $this->valuesdescription;
        echo $this->values;
    }

}

$grafico = new Grafico(1,2,3,4,5,6,7);
$grafico->printa();

Reading tip: When to use self vs $ this in PHP?

    
20.02.2017 / 14:44