Consider the following code:
class A {
private $a;
function f() {
A::a = 0; //error
$this->a = 0; //error
$self::a = 0; //error
}
}
How can I change $a in f()?
You were close. Either:
self::$a = 0; //or
A::$a = 0;
If it's static, or:
$this->a = 0;
If it's not.
I believe the syntax is:
self::$a
obviously we all have been tricked by the title of the question, though here is how you change $a's value.
<?php
class A {
private $a;
function f() {
//A::a = 0; //error
$this->a = 10; //ok
//$self::a = 0; //error
}
function display() {
echo 'a : ' . $this->a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
?>
Output:
a : 10
if you want $a to be static use the following:
<?php
class A {
private static $a;
function f() {
//A::$a = 0; //ok
//$this->a = 10; //Strict Standards warning: conflicting the $a with the static $a property
self::$a = 0; //ok
}
function display() {
echo 'a : ' . self::$a . '<br>';
}
}
$a = new A();
$a->f();
$a->display();
// notice that you can't use A::$a = 10; here since $a is private
?>
Related
$a = 0;
function a(){
global $a;
$success = b();
if ($success) {
return $a;
$a = 0;
}
}
function b(){
global $a;
$a += 9;
return true;
}
In function a if function b is success then i want to return $a and then set $a to 0
how can i do this ?
You could try creating a temporary variable and then set $a before returning.
$temp = $a;
$a = 0;
Return $temp;
This question already has answers here:
In PHP: How to call a $variable inside one function that was defined previously inside another function?
(2 answers)
Closed 5 years ago.
For instance I have the following class
class myClassName{
function funtionOne(){
$a = '123';
$b = '456';
$c = '789';
$d = 'var_value';
$e = 'var_value';
$f = 'var_value';
}
function funtionTwo(){
$a = '123';
$b = '456';
$c = '789';
$g = 'var_value';
$h = 'var_value';
$i = 'var_value';
}
}
As you can see the variables $a, $b, $c are in both methods. My question is: How can I extend a method so get something like this:
class myClassName{
function funtionABC(){
$a = '123';
$b = '456';
$c = '789';
}
function funtionOne(){
include functionABC's content here
$d = 'var_value';
$e = 'var_value';
$f = 'var_value';
}
function funtionOne(){
include functionABC's content here
$g = 'var_value';
$h = 'var_value';
$i = 'var_value';
}
}
Note: I do not want to return anything from the function, just want to retrieve the content, as you would with an include() or a trait. Thank you.
class myClassName{
public $a, $b, $c;
function Init_funtionABC(){
$this->a = '123';
$this->b = '456';
$this->c = '789';
}
function funtionOne(){
Init_funtionABC();
//you can access $this->a , $this->b , $this->c here
$d = 'var_value';
$e = 'var_value';
$f = 'var_value';
}
function funtionOne(){
Init_funtionABC();
//you can access $this->a , $this->b , $this->c here
$g = 'var_value';
$h = 'var_value';
$i = 'var_value';
}
}
this is a very simple program, but when i run it, it just out put the first echo statement, which is 3, others are not displaying. i declared it global in the function, why it dose not work, strangely, when i was coding another program declaring a global array variable in a function it works perfectly, please explain it in detail, thanks
$a = 1;
$b = 2;
$c = $a + $b;
echo $c ."<br>";
function aaa()
{
global $a;
global $b;
$d = $a + $b;
echo $a ."<br>";
function ccc()
{
global $d;
$e = $c + 1;
echo $e;
}
}
The problem with your code is that you've put the function ccc inside the funciton aaa and that is not the correct way of doing what you're trying to do.
The correct way would be to create a Class aaa and then declaring ccc as its method.
$a = 1;
$b = 2;
$c = $a + $b;
$d = 0;
function aaa() {
global $a;
global $b;
global $d;
$d = $a + $b;
echo $a ."<br>";
}
function ccc() {
global $c;
global $d;
$e = $c + 1;
echo $e;
}
echo $c ."<br>";
aaa();
ccc();
I have a problem with the work with variables from one condition in the second condition. I have something like that:
<form name="exampleForm" method="post">
...
<input type="submit" name="firstSubmit" value="Send">
<input type="submit" name="secondSubmit" value="Show">
</form>
<?php
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
if(isset($_POST['secondSubmit'])) {
echo $functionOutput;
}
?>
When I need to work with variable $functionOutput from first condition I always get an error message (undefined variable). How I can solve this problem?
I'm not sure what you are trying to do exactly, but when you press your second button, the variable $functionOutput is not defined as the first condition is false so that whole section is skipped.
Note that variables are lost as soon as the script ends. You could look into sessions and use session variables to solve that, but it depends a bit on what you want to do exactly.
To use sessions, you would have to move your whole php block to before where you start outputting html and do something like:
<?php
session_start();
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
return $c;
}
$_SESSION['output'] = a();
}
// start html output
?>
<doctype .....
<html ....
// and where you want to echo
if(isset($_POST['firstSubmit'])) {
echo "The result is {$_SESSION['output']}";
}
if(isset($_POST['secondSubmit'])) {
echo $_SESSION['output'];
}
<?php
$functionOutput = "";
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
if(isset($_POST['secondSubmit'])) {
echo $functionOutput;
}
?>
Should fix it. It's happening because you're declaring $functionOutput inside your first IF statement.
As $functionOutput is not initialized when you are calling if(isset($_POST['secondSubmit']))
<?php
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
$functionOutput='12';//intialize
if(isset($_POST['secondSubmit'])) {
echo $functionOutput;
}
?>
**OR**
<?php
if(isset($_POST['firstSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a + $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
}
if(isset($_POST['secondSubmit'])) {
function a() {
$a = 5;
$b = 6;
$c = $a - $b;
echo "The result is $c";
return $c;
}
$functionOutput = a();
echo $functionOutput;
}
?>
There are 3 compilation errors in the init.php code:
Undefined variable $ind
Undefined variable $popsize
Undefined variable $chrom
How to solve this issue in the proper way?
main.php
include_once 'init.php';
class Individual {
public $genes = array();
//...
}
class Population {
public $ind = array();
public $ind_ptr;
public function setIndPtr(Individual $ind) {
$this->ind_ptr = $ind;
}
}
$popsize = 10;
$chrom = 5;
$pop = new Population();
$pop_ptr = new Population();
$pop = init(pop_ptr);
init.php
function init(Population $pop_ptr) {
$pop_ptr->setIndPtr($ind[0]);
for ($i = 0 ; $i < $popsize ; $i++) {
for ($j = 0; $j < $chrom; $j++) {
$d = rand(0,1);
if($d >= 0.5) {
$pop_ptr->ind_ptr->genes[$j] = 1;
}
else {
$pop_ptr->ind_ptr->genes[$j] = 0;
}
}
$pop_ptr->setIndPtr($ind[$i+1]);
}
$pop_ptr->setIndPtr($ind[0]);
return $pop_ptr;
}
Its a matter of scope: Variables are not shared over files, unless you make them global!
(Badly explained) variables such as
inc.php
$a=1;
main.php
include "inc.php";
print $a
would work
however
inc.php
function func()
{
$a=1;
}
main.php
include "inc.php";
func();
print $a;
a is not available.
Hope that makes it clearer.
Global variables in function scope need to be declared explicitly global before use:
<?php
function foo()
{
global $global_variable_from_outside_function_scope;
$global_variable_from_outside_function_scope += 1;
}
As for $ind, there's no such variable there. You want something more akin to $pop_ptr -> ind. Read the PHP docs again on classes, scope, etc..