How to access public const LINE_DOUBLE using string of library phppresentation

68 views Asked by At

i want to access LINE_DOUBLE from the Border Library of phppresentation using string "LINE_DOUBLE" have tried using constant() but no luck any other solution will be very helpful

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Style\Border;
use PhpOffice\PhpPresentation\Style\Fill;

 public function addLine($val)
        {
          $border = new Border;
          $str = "LINE_DOUBLE";
          $lineShape = $this->currentSlide->createLineShape($val["startX"],$val["startY"],$val["endX"],$val["endY"]);
          $lineShape->getBorder()
          ->setLineStyle(constant("$border::$str"))
          ->setLineWidth($val["width"])
          ->getColor()->setARGB($val["lineColor"]);     
        }
2

There are 2 answers

9
Hari Darshan On

There are two ways

  1. Using constant() function
->setLineStyle(constant("Border::{$str}"))

$border is an object not class and to access the Class constant, you need Class instead of it's object

  1. Using Reflection
$ref = new ReflectionClass('Border');
->setLineStyle($ref->getConstant($str));

Let me know, if either of them works or not

0
user15398154 On

Just provide the full specified path of the obejct $str = "LINE_DOUBLE"; ->setLineStyle(constant("PhpOffice\PhpPresentation\Style\Border::$str"))