Jump to content

Use one variable value in another variable under same class


ankur0101

Recommended Posts

Hi,

I have following code :

<?php
class CategoryWork {
    
    public $form = "";
    public $error = "";
    public $add_form = "<br /><br /><p><strong>Add New Category</strong></p><br /><form id=\"form1\" name=\"form1\" method=\"post\" action=\"category.php?action=add\">
  <table width=\"550\" height=\"170\" border=\"0\">
    <tr>
      <td width=\"153\">Name :</td>
      <td colspan=\"2\"><label for=\"cat_name\"></label>
      <input name=\"cat_name\" type=\"text\" id=\"cat_name\" size=\"50\" maxlength=\"50\" /></td>
    </tr>
    <tr>
      <td>Slug :</td>
      <td colspan=\"2\"><label for=\"cat_slug\"></label>
      <input name=\"cat_slug\" type=\"text\" id=\"cat_slug\" size=\"50\" maxlength=\"50\" /></td>
    </tr>
    <tr>
      <td>Description</td>
      <td colspan=\"2\"><label for=\"cat_desc\"></label>
      <textarea name=\"cat_desc\" id=\"cat_desc\" cols=\"48\" rows=\"10\"></textarea></td>
    </tr>
    <tr>
      <td> </td>
      <td width=\"97\"><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></td>
      <td width=\"286\"><input type=\"reset\" name=\"button2\" id=\"button2\" value=\"Reset\" /></td>
    </tr>
     <tr>
      <td> </td>
      <td colspan=\"2\">error</td>
    </tr>
  </table>
</form>";
}
?>

 

At the line of <td colspan=\"2\">error</td>

I want to insert $error, hence I made the line as

<td colspan=\"2\">".$error."</td> and also tried <td colspan=\"2\">".$this->error."</td>

 

After executing with this, php says syntax error at the line of public $add_form

 

I am going to declare an error at the $error

 

When I do <td colspan=\"2\">$error</td>

it says : Parse error:  syntax error, unexpected '"' at line of public $add_form

 

When I do  <td colspan=\"2\">".$error."</td>

it says : Parse error:  syntax error, unexpected '.', expecting ',' or ';' at the above line i.e. of <td>

 

I am damn confused, I googled but nothing found any helpful material.

Anybody please help me..

 

Thanks

 

 

Link to comment
Share on other sites

<?php
class CategoryWork {
    
    public $form = "";
    public $error = "";
    public $add_form = "<br /><br /><p><strong>Add New Category</strong></p><br /><form id='form1' name='form1' method='post' action='category.php?action=add'>
  <table width='550' height='170' border='0'>
    <tr>
      <td width='153'>Name :</td>
      <td colspan='2'><label for='cat_name'></label>
      <input name='cat_name' type='text' id='cat_name' size='50' maxlength='50' /></td>
    </tr>
    <tr>
      <td>Slug :</td>
      <td colspan='2'><label for='cat_slug'></label>
      <input name='cat_slug' type='text' id='cat_slug' size='50' maxlength='50' /></td>
    </tr>
    <tr>
      <td>Description</td>
      <td colspan='2'><label for='cat_desc'></label>
      <textarea name='cat_desc' id='cat_desc' cols='48' rows='10'></textarea></td>
    </tr>
    <tr>
      <td> </td>
      <td width='97'><input type='submit' name='button' id='button' value='Submit' /></td>
      <td width='286'><input type='reset' name='button2' id='button2' value='Reset' /></td>
    </tr>
     <tr>
      <td> </td>
      <td colspan='2'>$this->error</td>
    </tr>
  </table>
</form>";
}
?>

 

Just replaced all \" with ', See if you still get the php error.

 

EDIT: I just used the class and didn't get the error

Link to comment
Share on other sites

Alternatively:

public $add_form = <<<_END
  <br /><br /

the rest of your form but without escping characters as in heredoc var

_END;

 

Putting all the form in a heredoc var will mean you can use double or single without having to worry about escaping anything.

Link to comment
Share on other sites

The problem is that you cannot use variable values or even the dot concatenation operator in the declaration of your class properties. You can only assign fixed/constant values when you declare a class property.

 

To do what you want, you will need to assign that string to the $add_form property at runtime using code.

Link to comment
Share on other sites

The problem is that you cannot use variable values or even the dot concatenation operator in the declaration of your class properties. You can only assign fixed/constant values when you declare a class property.

 

To do what you want, you will need to assign that string to the $add_form property at runtime using code.

 

Can you give me example of this ?

Link to comment
Share on other sites

You'll have to add that to the constructor:

class ThisIsMyClass {
  public $var1 = '';
  public $var2 = '';

  function __construct( $newvar ) {
    $this->var1 = $newvar;
    $this->var2 = 'This is my text being concatinated ' . $this->var1 . ' with other text.';
  }
}

Hope this sample code helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.