Create Instance variable in Laravel - php

I have a php class which having 4 methods.All the 4 methods are using some common variables, I've created those variables as instance variable. But i'm getting an error like "Undefined variable: " How can i solve this problem.
My code is,
public class test{
public static $variable;
public function func(){
$variable = "Hello World";
print_r($variable);
}
}

Actually this code will not gives you any error as you just print the variable which you have defined exactly above print statement.
Demo : http://sandbox.onlinephpfunctions.com/code/cd43e866591ee0693cdcbeec6a230f583f756a67
If you want to assign value to $variable which is defined above the function then try this code :
<?php
class test {
public static $variable;
public function func(){
self::$variable = "Hello World";
print_r(self::$variable);
}
}
$n = new test();
echo $n->func();
?>
Demo : http://sandbox.onlinephpfunctions.com/code/f11b726a678b9a5ee0e474d7ca194bb5ed75af22

If you have same static value for that variable try this
class test
{
const variable ="Hello World";
public function func()
{
echo self::variable;
}
}
$name = new test;
$name->func();

Related

PHP OOP print a variable which has value inside the function but call from outside to be print

Is it possible to print a variable which has the value inside the function but it's called from outside the function to be print in object oriented programming in PHP
Let's explain by example
My class looks like as:
class my {
public $a;
public function myFunc(){
$name = "fahad";
echo $this->a;
}
}
It should print the value of $name when the function is call, as I am trying:
$class = new my();
$class->a = '$name';
$class->myFunc();
But it did't work and print the result as:
$name
I want it should print the value of variable $name which is inside the function
How it can be possible?
Thank You.
You can use variable variables to do this, but it's usually considered bad practice.
class my {
public $a;
public function myFunc(){
$name = "fahad";
echo ${$this->a};
}
}
$class = new my();
$class->a = 'name';
$class->myFunc();
Output:
fahad
Inside your function, you can make a check:
public function myFunc(){
if($this->a == '$name'){
$name = 'fahad';
echo $name;
}else echo $this->a;
}

how to access variables in a class but outside a function?

i made the following code:
<?php
class hoi {
public $a = 1;
function test()
{
echo $this->$a; /* reference to alocal scope variable? */
}
}
$hoi = new hoi;
$hoi->test();
?>
I try to echo $a but this does not work,
how can i echo variables declared inside the class but outside the function?
The syntax is:
$this->a
Using an additional $ in there is a "variable variable" for properties.
class hoi {
public $a = 1;
function test() {
echo $this->a;/* the variable is accessed like this - no need for the $ */
}
}
$hoi = new hoi();/* required as there is no __construct() method */
$hoi->test();

Using local variable from call in a function

After 9 hours of struggling to get this right, I have turned to the internet for help. I can't seem to find any relevant answers doing a Google search.
I currently have a class called Test. Test accepts a single argument.
<?php
class test {
private $varpassed;
public function getVarpas() {
return $this->varpassed;
}
Public function setVarpas($value) {
$this->varpassed= $value;
}
public function stringGen(){
$testvar = $this->varpassed;
echo $testvar;
}
}
The stringGen function should return the $varpassed variable whenever its called. The value for $varpassed is set using the setVarpas function. However, when ever I call the stringGen() method I only seem to be getting the following error:
Fatal error: Using $this when not in object context in file.php line 14.
Pointing to this line:
$testvar = $this->varpassed;
Is there any other way to pass the variable to the stringGen method? I've tried using:
self::$this->varpassed;
Which also throws an error.
first create an instance of the object (so you can use $this in the context), for example:
$test = new test();
then you can call:
$test->setVarpas('Hello World!');
now you can call:
$test->stringGen();
you have to do something like this
$var = new test();
$var->setVarpas("Hello");
$var->stringGen(); // this will echo Hello
$this is used when you are withing class. outside class you have to use class object.
1) Change this: class test() to class test
2) Create and instance first something like $t1 = new test();
3) Call the function $t1->setVarpas(5);
4) Now you can call the function $t1->stringGen();
Fixed:
<?php
class test
{
private $varpassed;
public function getVarpas() {
return $this->varpassed;
}
Public function setVarpas($value) {
$this->varpassed= $value;
}
public function stringGen(){
$testvar = $this->varpassed;
echo $testvar;
}
}
$t1 = new test();
$t1->setVarpas(5);
$t1->stringGen();
OUTPUT:
5
You should not declare a class with parentheses.
Use
class test {
instead of
class test(){

Is it possible to change a property of a class outside of the class? (PHP)

I'm quite inexperienced with OOP PHP but here's my question...let's say I have this class with one property:
class myClass {
public $property = array();
public function getProperty() {
return $this->property;
}
}
How would it be possible to change the value of $property without altering the class itself in any way, or by instantiating an object out of it, then changing its property. Is there any other way of doing it? Using scope resolution?
Hope that makes sense, any help would be much appreciated.
What you want is a static member
class MyClass {
public static $MyStaticMember = 0;
public function echoStaticMember() {
echo MyClass::$MyStaticMember;
//note you can use self instead of the class name when inside the class
echo self::$MyStaticMember;
}
public function incrementStaticMember() {
self::$MyStaticMember++;
}
}
then you access it like
MyClass::$MyStaticMember = "Some value"; //Note you use the $ with the variable name
Now any instances and everything will see the same value for whatever the static member is set to so take for instance the following
function SomeMethodInAFarFarAwayScript() {
echo MyClass::$MyStaticMember;
}
...
MyClass::$MyStaticMember++; //$MyStaticMember now is: 1
$firstClassInstance = new MyClass();
echo MyClass::$MyStaticMember; //will echo: 1
$firstClassInstance->echoStaticMember(); //will echo: 1
$secondInstance = new MyClass();
$secondInstance->incrementStaticMember(); // $MyStaticMember will now be: 2
echo MyClass::$MyStaticMember; //will echo: 2
$firstClassInstance->echoStaticMember(); //will echo: 2
$secondInstance->echoStaticMember(); //will echo: 2
SomeMethodInAFarFarAwayScript(); //will echo: 2
PHPFiddle
I hope this is what you are looking for
<?php
class myClass {
public $property = array();
public function getProperty() {
print_r($this->property);
}
}
$a = new myClass();
$x = array(10,20);
$a->property=$x; //Setting the value of $x array to $property var on public class
$a->getProperty(); // Prints the array 10,20
EDIT :
As others said , yes you need the variable to be declared as static (if you want to modify the variable without creating new instance of the class or extending it)
<?php
class MyClass {
public static $var = 'A Parent Val';
public function dispData()
{
echo $this->var;
}
}
echo MyClass::$var;//A Parent Val
MyClass::$var="Replaced new var";
echo MyClass::$var;//Replacced new var
?>

Using a define (or a class constant) to call a variable method?

Is it possible to :
define('DEFAULT_METHOD', 'defaultMethod');
class Foo
{
public function defaultMethod() { }
}
$foo = new Foo();
$foo->DEFAULT_METHOD();
Or do I have to :
$method = DEFAULT_METHOD;
$foo->$method();
And what about a class constant instead of a define ?
If you use a variable or constant as the method name, you have to put it into curly brackets:
$foo->{DEFAULT_METHOD}();
The same technique works for variables, including static class attributes:
class Foo {
public static $DEFAULT_METHOD = 'defaultMethod';
public function defaultMethod() { echo "Cool!\n"; }
}
$foo = new Foo();
$foo->{FOO::$DEFAULT_METHOD}();
In fact, practically any expression that results in a valid method name could be used:
$foo->{'default'.'Method'}();
You could set it to a variable first as in your example :)
Example: http://codepad.org/69W4dYP1
<?php
define('DEFAULT_METHOD', 'defaultMethod');
class Foo {
public function defaultMethod() { echo 'yay!'; }
}
$foo = new Foo();
$method = DEFAULT_METHOD;
$foo->$method();
?>

Categories