Constructor php

Asked

Viewed 923 times

3

I’m 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 still, but is returning me this error:

Notice: Undefined variable: typegraph in C: Inetpub wwwroot HOPE php Graficos.php on line 23

I’ve tried the methods GETTERS and SETTERS and yet I couldn’t solve.

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

}
  • To take class property, use $this->nome_da_proriedade and the printar method you can subistituir by __toString()

2 answers

8


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

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, Cvoce is referencing the class variable, the this kind of points to the class itself, actually it’s the class instance.

The same thing goes for the method Grafico

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 to reinforce, what you were doing was creating variables within the method while class properties remained "untouched".

Read the PHP manual for this property session, will help a lot to understand the concept.

  • Guy worked really thanks, I tried with the $this on the front, however my mistake was to put the $ in front. Could explain me why I did not have the $?

  • 1

    tried $this in front of what? Soon in the beginning kind, private $this->values=0;? That was it?

  • I tried exactly the way you said but I was doing so $this->$typegraph = $typegraph_p; both in the graphic method and in the method to get the value.

  • 1

    The syntax is $this->propriedadedoobject . $this->$variable is incorrect . Tb confuses me at first. :)

  • 1

    if you tried something like $this->$values it will try to fetch a variable in the local scope called "values" and give it 1 toString and fetch a property from the "toStringed" value class, then if it does not find in the local scope the "$values" it will do something like $this->null what must have generated your error.

  • 1

    There are those who use tb __Construct instead of the class name for the constructor. I am one of those people. Also tb use __destruct in generic controller classes that I do other classes my extenderem. In this __destruct I call a view with the same name as the method(action) that is being accessed.

Show 1 more comment

3

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

Another detail is that you are trying to start the class constructor with a method of the same name as her, such as JAVA and which has been declared obsolete from the 5.3.3 and becoming a common method, when the __construct.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.