Why does this not make a difference between property and variable? - php

class someclass
{
public $foo = 'abcd';
public function __construct($data)
{
$this->foo = $data;
}
public function doSomething()
{
$user = $_POST['username'];
echo $foo = $_POST['foo']; // This output correct value
var_dump($foo); // This Output NULL
$somethingelse = $_POST['foo'];
var_dump($somethingelse); // Output as expected
}
}
If i change my variable name or property name from $foo to something else in do in doSomething() it runs fine.
Why do I need to keep the property name and variable name different here?
Why does $foo is NULL when one of the property name is $foo?

You need to use $this->foo to get and set the classes property

change this
echo $foo = $_POST['foo'];
to
echo $this->foo = $_POST['foo'];
var_dump($this->foo);

When accessing class variables you need to use the $this-> prefix.
Change your code to
echo $this->foo = $_POST['foo'];
var_dump($this->foo);

It is correct and it works fine. I ran your code and it always gives me the same. There's no problem you have property $foo and $foo variable in one or multiple functions. It always give me the same answer.
If $_POST['foo']=test then echo $foo = $_POST['foo']; returns "test", $foo returns "test" and $somethingelse returns "test";

Related

Access static object property through variable name

I know its possible to access an object property/method using a variable as its name
ex.:
$propName = 'something';
$something = $object->$propName;
Is it possible to do the same w/ constants or static properties?
I've tried:
$constName = 'MY_CONST';
MyCLass::{$constName};
and
$obj::{$constName};
But nothing seems to work and I couldn't find it anywhere.
Use: Class::$$constName, this is similar to normal variable variables.
Demo:
<?php
class MyClass {
public static $var = 'A';
}
$name = 'var';
echo MyClass::$$name; // echoes 'A'
Constants can be access with the constant function:
constant('MyClass::'.$constantName)
This works for me:
<?php
class Test {
public static $nombre = "pepe";
public function __construct() {
return self;
}
}
$varName = "nombre";
echo Test::${$varName};
You can use the constant function:
constant('bar::'. $const);
constant("$obj::". $const); //note the double quote

Difference between $object->lol and $object->$lol

I am having a confusion between this two kinds of code and want to know, what is the difference between them:
$object->$lol
// and
$object->lol
$object->lol will points to some property as defined on the object class:
class SomeClass {
public $lol = 'some value';
}
$object = new SomeClass();
echo $object->lol; //will yield 'some value'
And $object->$lol will point to some property as defined on the object class, but will access the correct property based on the value for $lol:
class SomeClass {
public $lol = 'some value';
public $random = 'random value';
}
$lol = 'random';
$object = new SomeClass();
echo $object->lol; //will yield 'some value'
echo $object->$lol; //will yield 'random value'
$object->lol access a instance variable named lol on the $object object.
$object->$lol access a instance variable that has the name contained in the $lol variable. It is also accessd on the $object object.
AFAIK, it works the same as the following:
$var = "var2";
$var2 = "hello world";
echo $$var; // This returns "hello world"
So, I assume that $object->$lol gets you the variable with the name of your $lol variable.
Here is some more info about this...
First one is like
$lol = 'foo';
$object->{$lol} == $object->foo;
Second one is regular property access.
I will explain you this,look this code:
<?php
$var1->"hello";
$var2->$var1;
?>
If we do a print or an echo $var1 will show "hello" but in $var2 we will see "hello" again because $var1 and $var2 are the same.
In the second code:
<?php
$var1->"hello";
$var2->"var1";
?>
Now we print $vare1 and it'll show "hello" but $var2 will show "var1".I hope this help you to solve your problem.

change and initialise variables in a function

I was wondering if it's possible to change and initialize variables in a function without passing arguments to the function. Here is what I want to achieve:
$foo = 'Lorem';
$array = array();
foobar($foo);
function foobar(){
if (strlen($foo)== 1)
$bar = 'Ipsum';
else
$array[] = 'error';
}
fubar();
function fubar(){
if (empty($fouten))
echo $bar;
}
$foo is a local (uninitialized) variable inside a function. It is different from the global variable $foo ($GLOBALS['foo']).
You have two ways:
$foo;
$bar;
$array = array();
function foobar(){
global $foo, $array, $bar;
if (strlen($foo)== 1)
$bar = 'Ipsum';
else
$array[] = 'error';
}
or by using the $GLOBAL array …
This is not really good practice though and will become a maintenance nightmare with all those side effects
Functions in php can be given arguments that have default values. The code you posted as written will give you notices for undefined variables. Instead, you could write:
function foobar($foo = null) {
if($foo) { // a value was passed in for $foo
}
else { // foo is null, no value provided
}
}
Using this function, neither of the below lines will produce a notice
foobar();
foobar('test');

PHP functions 101 question, really simple

I'm kind of slow in the head so can someone tell me why this isn't working:
function foo() {
$bar = 'hello world';
return $bar;
}
foo();
echo $bar;
I just want to return a value from a function and do something with it.
Because $bar does not exist outside of that function. Its scope is gone (function returned), so it is deallocated from memory.
You want echo foo();. This is because $bar is returned.
In your example, the $bar at the bottom lives in the same scope as foo(). $bar's value will be NULL (unless you have defined it above somewhere).
Your foo() function is returning, but you're not doing anything with it. Try this:
function foo() {
$bar = 'hello world';
return $bar;
}
echo foo();
// OR....
$bar = foo();
echo $bar;
You aren't storing the value returned by the foo function
Store the return value of the function in a variable
So
foo() should be replaced by
var $t = foo();
echo $t;
As the others have said, you must set a variable equal to foo() to do stuff with what foo() has returned.
i.e. $bar = foo();
You can do it the way you have it up there by having the variable passed by reference, like so:
function squareFoo(&$num) //The & before the variable name means it will be passed by reference, and is only done in the function declaration, rather than in the call.
{
$num *= $num;
}
$bar = 2;
echo $bar; //2
squareFoo($bar);
echo $bar; //4
squareFoo($bar);
echo $bar; //16
Passing by reference causes the function to use the original variable rather than creating a copy of it, and anything you do to the variable in the function will be done to the original variable.
Or, with your original example:
function foo(&$bar)
{
$bar = 'hello world';
}
$bar = 2;
echo $bar; //2
foo($bar);
echo $bar; //hello world

How to dynamically assign a value to a class property in PHP?

I would like to assign a value in a class property dynamically (that is referring to it using a variable).
#Something like:
setPropValue($obj, $propName, $value);
$obj->$propName = $value;
In case you want to do this for static members, you can use variable variables:
class Foo
{
public static $foo = 'bar';
}
// regular way to get the public static class member
echo Foo::$foo; // bar
// assigning member name to variable
$varvar = 'foo';
// calling it as a variable variable
echo Foo::$$varvar; // bar
// same for changing it
Foo::$$varvar = 'foo';
echo Foo::$$varvar; // foo
Like this?
$myClass = new stdClass();
$myProp = 'foo';
$myClass->$myProp = 'bar';
echo $myClass->foo; // bar
echo $myClass->$myProp; // bar

Categories