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'

?>

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.