0

hi, am in learning phase of php , am struck here , i try to call method in a class from other class namely display.phtml and displaycall.phtml
i was successfull in calling the method but problem is , i have a variable $_productCode with value equal to 'A1001'. The main part of this problem is the $_productCode variable is outside the class display, under this circumstances , its value is not available inside the class , so if i run the displaycall.phtml , it neglect the $_productCode part of display ,becoz it did not get the value of $_productCode .

Is there any way to acheive it , please help me out. following are the files of code.

display.phtml

<?php

$_productCode = 'A1001';
class display 
{
var $itemvalue;
var $_productCode ;
function getData()
{
 $itemvalue = $_productCode ;
 echo $itemvalue ;
 echo "***********";

}
}

$display = new display();
$display->getData();

?>

displaycall.phtml

<?php
include "display.phtml";

class displaycall extends display
{
    public function printItem($string)
    {
		echo 'displaycall: ' . $string . '';
    }
}

$display = new display();
$displaycall = new displaycall();
 // Output: 'display: baz'
 // Output: 'PHP is great' 
$displaycall->printItem('baz'); // Output: 'Bar: baz'
 // Output: 'PHP is great'

?>

Edited by goldy736: n/a

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by jomanlk
0

If you really want to access that variable from outside the proper scope (the correct way would be to encapsulate in a class or put it in config) the use the global keyword.

Whenever you want to use the value stored inside that variable globally use

global $_productCode;

... and the value is copied over.

However, this is not encouraged since you can have weird effects if variable values are modified unintentionally.

Hope this helps.

0

wtf? double post again. Mods, please delete. Sorry, but Chrome is giving me troubles with the quick reply :/

Edited by jomanlk: Double post.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.