Dynadot โ€” .com Registration $8.99

PHP: How to pass a variable in a function of a class?

Spaceship Spaceship
Watch

epamnp

Established Member
Impact
0
I have a variable $my_var and I want to pass it in the function Disp(). What is the way to do it?

<?php
class Page
{
public function __set($name, $value)
{
$this->$name=$value;
}
public function Disp()
{
echo "My first Page" ;
}
}
?>

<?php
$my_var = 'this is a msg' ; // I want to pass this variable in function Disp()
class NextPage extends Page
{
public function Disp()
{
echo $my_var ; // I want to pass the variable $my_var here
}
}
$np = new NextPage();
$np->Disp();
?>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
<?php 
$my_var = 'this is a msg' ; // I want to pass this variable in function Disp()

class NextPage extends Page
     {
          public function Disp($foo)
               {
                    echo $foo; // I want to pass the variable $my_var here
               }
     }

$np = new NextPage();
$np->Disp($my_var);
?>
 
0
•••
Thank you Mikor.
 
Last edited:
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back