Common class usage in view

Hi all,

I have created a common class to contain all the code to plot x,y maps and render them in svg.

I am testing various map building functions and am trying to invoke one of these functions in the main app index.php

Here is the class:


class omegaMap {

    

    public $x;

    public $y;

    

    public $map;

    

    public function omegaMap($xin, $yin)

      {

		$x=$xin;

		$y=$yin;

      }

    

     

    public function testMap ($width, $height) {

      $map = "<svg xmlns=\"xxxxxxxxxxxxx" width=\"".$width."px\" height=\"".$height."px\" viewBox=\"0 0 ".$width." ".$height."\">\n" ;

      $map .= "<g transform=\"translate(0,$height) scale(1,-1)\">\n";

      $map .= "<circle cx=\"".$x."\" cy=\"".$y."\" r=\"3\" fill=\"red\"/>\n";

      $map .= "</g>\n</svg>\n";

      return $map;

    }

}

I am trying to get the string $map in index.pho this way:


<p><?php $map = new omegaMap(200,200);

         echo $map::testMap(400,400); ?></p>

The $x and $y in the class are not being stored and are unassigned in the function call testMap.

Any help or thoughts would be a appreciated.

Hi welcome to the Yii forum…

The contruction method in PHP is __construct() - http://php.net/manual/en/language.oop5.decon.php

I tried using this before:


public function __construct($x,$y) {

  $x = $xin;

  $y = $yin;

}



When this line is executed:


map .= "<circle cx=\"".$x."\" cy=\"".$y."\" r=\"3\" fill=\"red\"/>\n";

an error is generated " Undefined variable: x "

When I use this constuctor:


public function __construct($x,$y) {

  $this->x = $xin;

  $this->y = $yin;

}

PHP looks to SiteController for $this->x

You need to learn first some basics about PHP…

Check this documentation - http://www.php.net/manual/en/language.oop5.properties.php

$this refers to the current object…

So your code should be




public $x;

public $y;

public function __construct($xin,$yin) {

  $this->x = $xin;

  $this->y = $yin;

}

Evidently not in yii. Referencing $this-x in the circle generation statement has PHP looking in the SiteController class for x.

it works even in Yii… but you have some other error…

Note that in your original post there are other errors too… like:

you have declared a public $map… but in the testMap() you use a local variable $map instead of the class variable $this->map

Again in the testMap() you need to use $this->x

The first line in testMap has an double quote not escaped (\"xxxxxxxx" <- last one is not escaped)

And testMap is not static so you cannot call it like


echo $map::testMap(400,400);

, but as


echo $map->testMap(400,400);

If you still don’t get it working please post here your current class code and how you call it…

Thanks for your help.

Here is the class:


class omegaMap {

    

    public $x;

    public $y;

    

    public $map;

    

    public function __construct($xin, $yin)

      {

		$this->x=$xin;

		$this->y=$yin;

      }

    

     

    public function testMap ($width, $height) {

      $map = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"".$width."px\" height=\"".$height."px\" viewBox=\"0 0 ".$width." ".$height."\">\n" ;

      $map .= "<g transform=\"translate(0,$height) scale(1,-1)\">\n";

      $map .= "<circle cx=\"".$x."\" cy=\"".$y."\" r=\"3\" fill=\"red\"/>\n";

      $map .= "</g>\n</svg>\n";

      return $map;

    }

}

Here is how I called it:


<p><?php $map = new omegaMap(200,200);

         echo $map->testMap(400,400); ?></p>



Here is the error:

On the line -> <circle cx=\"".$x."\" cy=\"".$y."\" r=\"3\" fill=\"red\"/>

Undefined variable: x

$this->x

$this->y

/Tommy

Thanks.