-1

I know that this as a question is more than duplicated, I've been reading around but due to my lack of expertise I haven't been able to apply any of the solutions to my case, actually, I did a similar question here where I partially solved my problem, so here's the thing:

File vars.php

<?php
$var = array(
"var1" => "Sample text1",
"var2" => "Sample text2",
"var3" => "Sample text3",
"var4" => "Sample text4",
"var5" => "Sample text5",
"var6" => "Sample text6",
);
?>

File apply.php

<?php
include ("vars.php");
class mouth {
private $teeth = array(
FIRST_EX=>'first text',
SECOND_EX=>'second text'
);
public $forms = array(
 'signin'=>array(
            'fields'=>array(
                'username'=>array('type'=>'text','placeholder'=>'third text','icon'=>'envelope'),
                'password'=>array('type'=>'password','placeholder'=>'fourth text','icon'=>'lock')
            ),
            'submit'=> 'fifth text',
            'message'=> 'sixth text'
        );
?>

So all of the texts (first text, second text, third text, etc.) in file apply.php should be respectively changed by $var['var1'], $var['var2], $var['var3], etc. to display, also respectively, Sample text1, Sample text2, Sample text3, etc.

So file apply.php should finally be more or less like this:

<?php
include ("vars.php");
class mouth {
private $teeth = array(
FIRST_EX=>'$var['var1']',
SECOND_EX=>'$var['var2']'
);
public $forms = array(
 'signin'=>array(
            'fields'=>array(
                'username'=>array('type'=>'text','placeholder'=>'$var['var3']','icon'=>'envelope'),
                'password'=>array('type'=>'password','placeholder'=>'$var['var4']','icon'=>'lock')
            ),
            'submit'=> '$var['var5']',
            'message'=> '$var['var6']'
        );
?>

Can somebody guide me in how should the variables be represented to achieve this objective.... thanks a lot

Andrés Chandía
  • 999
  • 1
  • 16
  • 32
  • Should your properties really be static, or do they have to be instanciated? – Ethan Sep 03 '18 at 17:56
  • Possible duplicate of [Array/classes Constant expression contains invalid operations](https://stackoverflow.com/questions/41983787/array-classes-constant-expression-contains-invalid-operations) – Progman Sep 03 '18 at 17:58
  • You can't access $var inside the class and you can't declare class properties with values from other variables. – Devon Bessemer Sep 03 '18 at 17:59
  • @Davіd, as I put in the question, no expertise in object oriented php, so I wouldn't know how to answer your question – Andrés Chandía Sep 03 '18 at 18:05

3 Answers3

1

Well first off you can't inject variables into the declaration body of a class. This part of the class is parsed by PHP when it compiles the code, so you can't directly modify it after that. And when it's being processed its way before you have set any variables.

Fortunately we have these things called methods, they are like functions but are in a class (they are even written as function), and some people just call them functions. Sometimes I find myself calling them that.

Anyway:

class Mouth
{ //capitalize class names

    public function createTeeth($var1, $var2)
    {
        $teeth = array(
            FIRST_EX  => $var1,
            SECOND_EX => $var2
        );
    }

    public function createForms($var3, $var4, $var5 $var6)
    {
        $forms = array(
            'signin' => array(
                'fields' => array(
                    'username'    => array(
                    'type'        => 'text',
                    'placeholder' => $var3,
                    'icon'        => 'envelope'
                ),                               
                'password' => array(
                         'type'        => 'password' ,
                         'placeholder' => $var4, 
                         'icon'        => 'lock'
                     )
                ),
                'submit'  => $var[ 'var5' ],
                'message' => $var[ 'var6' ]
             )
        );

    }
}

Then we call it like this

  $Mouth = new Mouth();
  //then call them
  $Mouth->createTeeth($var['var1'], $var['var2']);

You're quoting is all wrong too. now I just set these as local variables in the class. If you want to set them as properties, do like this:

 class Mouth
 { //capitalize class names

    private $teeth = [];

    public function createTeeth($var1, $var2)
    {
        $teeth = array(
            FIRST_EX  => $var1,
            SECOND_EX => $var2
        );
    }
   ...
 }

If you want the output them return them.

 class Mouth
 { //capitalize class names

    private $teeth = [];

    public function createTeeth($var1, $var2)
    {
         return array(
            FIRST_EX  => $var1,
            SECOND_EX => $var2
        );
    }
   ...
 }

Or any combination of the above. You can also make a function for each if you want

 class Mouth
 { //capitalize class names

    private $teeth = [];

    public function setTeeth($var1, $var2)
    {
         $this->teeth = array(
            FIRST_EX  => $var1,
            SECOND_EX => $var2
        );
    }

    public function getTeeth()
    {
        return $this->teeth;
    }
   ...
 }

This is what is known as dependency injection. Basically you are injecting values or stuff, the class depends on.

Hope that helps, Oh and I added a comment to your other question too.

As noted in the other answer you can pass them in as a constructor argument too, a lot of making that choice depends on how you use them. If they are required at the same time in the code you might wan't to do that.

Personally I like breaking it down where the arguments are named, it helps my IDE ( integrated development environment ). Which is just a fancy editor that does things like auto complete etc. So when I have them as arguments and I use the class the IDE tells me what the arguments are. like this (if I put the DOC comments in):

class Mouth
{ 

    /**
     * 
     * @var array
     */
    private $teeth = [];

    /**
     * populate the teeth array
     * 
     * @param string $var1
     * @param string $var2
     */
    public function setTeeth($var1, $var2)
    {
        $this->teeth = array(
            FIRST_EX  => $var1,
            SECOND_EX => $var2
        );
    }

    /**
     * returns the teath array
     * 
     * @return array
     */
    public function getTeeth()
    {
        return $this->teeth;
    }
    ...
}

And then it (IDE) tells me what that stuff is. If you get a chance check out Eclipse PDT, that's the editor I use, it's free, and it will help you with your coding.

P.S. I had to make it all PSR2 for you https://www.php-fig.org/psr/psr-2/

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

You can pass it as part of the constructor as follows

class mouth {
    private $var;

    public function __construct($var) {
        $this->var=$var;
    }
.....
}

and pass it when instantiating the class

$var = array(
 "var1" => "Sample text1",
 "var2" => "Sample text2",
 "var3" => "Sample text3",
 "var4" => "Sample text4",
 "var5" => "Sample text5",
 "var6" => "Sample text6",
);

$mouth = new mouth($var);

Inside the class methods you should use $this->var['var3'] instead of $var['var3']

Rinsad Ahmed
  • 1,877
  • 1
  • 10
  • 28
0

You can also require or include the vars in the constructor of mouth class as shown below. First, return the array in vars file

<?php
  return array(
    "var1" => "Sample text1",
    "var2" => "Sample text2",
    "var3" => "Sample text3",
    "var4" => "Sample text4",
    "var5" => "Sample text5",
    "var6" => "Sample text6",
  );
?>

Then require the vars file

 class mouth{
   private $var;

   public function __construct() {
     $this->var=require('vars.php');
   }
  .....
 }

Finally, you can now apply the values from the $var array, using

$this->var['var1']
Donnicias
  • 166
  • 7