What do this reference symbol (&) do in &methodName() method?
Is it necessary?
Is this called a reference by method?
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &methodName(){
return $this->value;
}
}
echo (new TestClass())->methodName(); //Outputs 5;
It means that the method returns a reference. If you then assign that to a reference variable outside the method, updating the variable will update the property.
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &refMethod(){
return $this->value;
}
public function valueMethod() {
return $this->value;
}
public function printValue() {
echo $this->value . "<br>";
}
}
$c = new TestClass();
$var1 = &$c->valueMethod();
$var1 = 10;
$c->printValue(); // prints 5
$var = &$c->refMethod();
$var = 20;
$c->printValue(); // prints 20
Related
In PHP I can use \ArrayAccess to create an object such that it can be assigned as variable (an array) but also execute some other stuff while doing it, is it possible to do the same with a single value variable?, for example:
$a = new MyClass(5, "some other stuff")->print(); //Prints 'some other stuff'
$b = $a * 2;
echo $b; //Prints 10
The idea would be that the first parameter is returned when the variable is assigned but at the same time I can do something else with the second parameter, so far I have only manage to do it ussing the __invoke magic method, but the end result is something like this:
$a = new MyClass(5, "some other stuff")->print(); //Prints 'some other stuff'
$b = $a() * 2;
echo $b; //Prints 10
Is it possible to do it without the __invoke method?
This is the way I managed to do it with \ArrayAccess (just in case it helps)
class MySQLK_customArray implements \ArrayAccess{
private $storage = array();
private $queryk;
public function __construct($fetch_array, $query){
$this->queryk = $query;
$this->storage = $fetch_array;
}
public function offsetSet($key, $value)
{
if (is_null($key)) {
$this->storage[] = $value;
} else {
$this->storage[$key] = $value;
}
}
public function offsetExists($key)
{
return isset($this->storage[$key]);
}
public function offsetUnset($key)
{
unset($this->storage[$key]);
}
public function offsetGet($key)
{
if (! isset($this->storage[$key])) {
return null;
}
$val = $this->storage[$key];
if (is_callable($val)) {
return $val($this);
}
return $val;
}
public function print(){
echo $this->queryk.";";
return $this;
}
public function get(){
return $this->storage;
}
}
It can be used this way:
$arr = new MySQLK_customArray(array("id" => 1),"SELECT * FROM Table LIMIT 1")->print(); //Prints 'SELECT * FROM Table LIMIT 1'
echo $arr['id']; //Prints 1
Are there any actual difference between the two ways to get the value by reference?
Way 1
<?php
class foo {
public $value = 42;
public function &getValue() {
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->getValue();
// $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;
// prints the new value of $obj->value, i.e. 2.
?>
Way 2
<?php
class foo {
public $value = 42;
public function getValue() {
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->value;
$obj->value = 2;
echo $myValue;
?>
In both cases 2 is printed. So why does one need the getValue() function then? The first example is taken from the PHP Manual.
You need the first approach if class fields don't have a modifier 'public'. In this case you can't get a reference to the field outside the class. See example:
<?php
class foo
{
protected $value = 1;
public function setValue($value)
{
$this->value = $value;
}
public function &getValue()
{
return $this->value;
}
}
$obj = new foo;
$myValue = &$obj->getValue();
$obj->setValue(2);
echo $myValue;
?>
class A{
public $name;
public function __construct() {
$this->name = 'first';
}
public function test1(){
if(!empty($_POST["name"]))
{
$name = 'second';
}
echo $name;
}
$f = new A;
$f->test1();
Why don't we get first and how set right default value variable $name only for class A?
I would be grateful for any help.
You can use a constructor to set the initial values (or pretty much do anything for that matter) as you need to like this:
class example
{
public $name;
public function __construct()
{
$this->name="first";
}
}
Then you can use these default values in your other functions.
class example
{
public $name;
public function __construct()
{
$this->name="first";
}
public function test1($inputName)
{
if(!empty($inputName))
{
$this->name=$inputName;
}
echo "The name is ".$this->name."\r\n";
}
}
$ex=new example();
$ex->test1(" "); // prints first.
$ex->test1("Bobby"); // prints Bobby
$ex->test1($_POST["name"]); // works as you expected it to.
you have two options to set the default values for the class attributes:
Option 1: set at the parameter level.
class A
{
public $name = "first";
public function test1()
{
echo $this->name;
}
}
$f = new A();
$f->test1();
Option 2: the magic method __construct() always being executed each time that you create a new instance.
class A
{
public $name;
public function __construct()
{
$this->name = 'first';
}
public function test1()
{
echo $this->name;
}
}
$f = new A();
$f->test1();
Use isset() to assign a default to a variable that may already have a value:
if (! isset($cars)) {
$cars = $default_cars;
}
Use the ternary (a ? b : c) operator to give a new variable a (possibly default) value:
$cars = isset($_GET['cars']) ? $_GET['cars'] : $default_cars;
I need to assign b value in a inside the method onec, but its failing. Please let me know what I am doing wrong here:
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$this->a = $this->b;
return $this->a;
}
}
echo One::onec();
?>
Use the self keyword. The $this keyword is not accessible under static context. Also, you should make your variables static
Like this..
<?php
class One {
public static $a = 10;
public static $b = 20;
public static function onec() {
self::$a = self::$b;
return self::$a;
}
}
echo One::onec();
You use $this in static function.
http://www.php.net/manual/en/language.oop5.static.php
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$obj = new One();
$obj->a = $obj->b;
return $obj->a;
}
}
echo One::onec();
Use this code
class One {
public $a = 10;
public $b = 20;
public function onec() {
$this->a = $this->b;
return $this->a;
}
}
$obj = new One();
echo $obj->onec();
the code explains it better:
class Class{
$var = 0;
function getvar()
echo $this->var;
}
}
$inst1 = new Class();
// I want to change $var here to 5
$inst2 = new Class();
echo $inst2->getvar(); // should be 5
Is it possible
Static. http://php.net/manual/en/language.oop5.static.php
class MyClass {
public static $var = 0;
function setVar($value) {
self::$var = $value;
}
function getVar() {
return self::$var;
}
}
echo MyClass::$var;
MyClass::setVar(1);
echo MyClass::getVar(); //Outputs 1
You should be able to do this using a static member variable.
class foo {
private static $var;
public static setVar($value) {
self::$var = $value;
}
public static getVar() {
return self::$var;
}
}
$a = new foo;
$a::setVar('bar');
$b = new foo;
echo $b::getVar();
// should echo 'bar';
You should declare $var to be static:
A data member that is commonly
available to all objects of a class is
called a static member. Unlike regular
data members, static members share the
memory space between all objects of
the same class.
You can use static variables:
class AAA{
public static $var = 0;
function getvar() {
return AAA::$var;
}
}
AAA::$var = "test";
$a1 = new AAA();
var_dump($a1->getvar());
var_dump(AAA::$var);