How can I define a variable before or while initializing the class?
<?php
class class{
public $var;
public function __construct(){
echo $this -> var;
}
}
$class = new class;
$class -> var = "var";
?>
If you mean instantiating the class, then use the constructor:
class Foo {
private $_bar;
public function __construct($value) {
$this->_bar = $value;
}
}
$test = new Foo('Mark');
You can do it 2 ways - see this example:
class bla {
public static $yourVar;
public function __construct($var) {
self::yourVar = $var
}
}
// you can set it like this without instantiating the class
bla::$yourVar = "lala";
// or pass it to the constructor while it's instantiating
$b = new bla("lala");
The first part you can only do with a static, but if you don't want to use a static, you'll have to initialize it via the constructor.
Hope that's what you were looking for...
$myVariable; // variable is defined
$myVariable = new myClass(); // instance of a class
class myClass {
protected $theVariable;
protected function myClass($value) {
$this->$theVariable = $value;
}
}
$theVariable = 'The Value';
$theClass = new myClass($theVariable);
echo $theClass->theVariable;
Related
I read that objects (instantiations of classes) are of global scope. This makes sense because objects (again, I am not talking about classes) should be easy to be reused in other parts of the code. But it seems that this information is wrong. Look at this example:
<?php
class test {
public $returnvalue = "foobar";
}
$testobject = new test();
class getvalue {
private $var;
function printvalue() {
$var = $testobject->returnvalue;
print "$var";
}
}
$getvalueobject = new getvalue();
$getvalueobject->printvalue();
?>
This code returns the error:
Trying to get property of non-object in ...
So, what would I have to do to make the $testobject (instantiation of class "test") available in class getvalue?
Thanks in advance.
<?php
class test {
public $returnvalue = "foobar";
}
//$testobject = new test(); MOVE INTO METHOD
class getvalue {
private $var;
function printvalue() {
$testobject = new test(); //For instance here
$var = $testobject->returnvalue;
print "$var";
}
}
$getvalueobject = new getvalue();
$getvalueobject->printvalue();
?>
Here is the documentation: http://php.net/manual/en/language.variables.scope.php
<?php
class test {
public $returnvalue = "foobar";
}
$testobject = new test();
class getvalue {
public $classObject;
function printvalue() {
$var = $this->classObject->returnvalue;
print "$var";
}
}
$getvalueobject = new getvalue();
$getvalueobject->classObject = $testobject;
$getvalueobject->printvalue();
?>
Example with class extend
<?php
class test {
public $returnvalue = "foobar";
}
// If you use extends in PHP classes you can access all variables and methods from the base class (In this example the base class would be Test)
class getvalue extends test {
function printvalue() {
// Because we extended the base class Test, we can access it's public variable returnvalue
$var = $this->returnvalue;
print "$var";
}
}
$getvalueobject = new getvalue();
$getvalueobject->printvalue();
?>
I was wrong. Objects do not have global scope.
So if you need to use an object in another class then you have to pass it either to the __construct function and store the object in the class properties or you have to pass it to the function where it is used:
<?php
class test {
public $returnvalue = "foobar";
}
$testobject = new test();
class getvalue {
private $var;
private $testobject;
function __construct($testobject) {
$this->testobject = $testobject;
}
function printvalue() {
$var = $this->testobject->returnvalue;
print "$var";
}
}
$getvalueobject = new getvalue($testobject);
$getvalueobject->printvalue();
?>
or
<?php
class test {
public $returnvalue = "foobar";
}
$testobject = new test();
class getvalue {
private $var;
function printvalue($testobject) {
$var = $testobject->returnvalue;
print "$var";
}
}
$getvalueobject = new getvalue();
$getvalueobject->printvalue($testobject);
?>
Is it possible to declare an object inside another class? The following code keeps giving me an error nexpected 'new' (T_NEW) error.
Class class1{
public function doSomething(){
$var = 3;
return true;
}
}
Class class2{
public $class1 = new class1();
public function doSomethingElse(){
if($class1->doSomething() == true){
return 10;
}else{
return 13;
}
}
}
//$obj = new class2();
I don't really want want to pass in the object through a constructor, because it's used inside other classes, so I'd have to pass it through multiple times. Is there a better method?
Use the Constructor of your class to instantiate the other class.
Class class1
{
public function doSomething()
{
$var = 3;
return true;
}
}
Class class2
{
protected $class1 = null;
public function __construct()
{
$this->class1 = new class1();
}
public function doSomethingElse()
{
if ($this->class1->doSomething() == true) {
return 10;
} else {
return 13;
}
}
}
Yes, but you have to put the initialization in construction method.
Class class2{
public $class1;
public function __construct() {
$this->class1 = new class1();
}
// ...
}
You can only initialize scalar values and arrays, use the constructor:
class Class2 {
public $class1;
public function __construct() {
$this->class1 = new Class1();
}
...
}
we have two class A & B:
class A{
var $settings;
function getinfo(){
$settings['domain']="mydomain";
$settings['pass']="1234";
return $settings;
}
}
class B extends A{
$ads = A::getinfo();
function makeurl(){
return "template/".$ads['domain'];
}
}
now i have an instance of B in my page, but i need "pass" , maybe some code like this:
$theme=new B();
$mypass = $theme->A->getinfo;
echo $mypass[$pass];
I know this code is full of faults , but i could not write a better one. is there any solution to access to password without making an instance of A?
Yes. It is as simple as this:
$theme = new B();
$mypass = $theme->getinfo();
echo $mypass['pass'];
You can also improve your classes a bit:
class A
{
var $settings;
function getinfo()
{
$this->settings['domain'] = "mydomain";
$this->settings['pass'] = "1234";
return $this->settings;
}
}
class B extends A
{
function makeurl()
{
$this->getinfo();
return 'template/' . $this->settings['domain'];
}
}
Why not call the settings variable in A from the B instance since B is a subclass of A?
Try this code:
<?php
class A
{
var $settings;
function getinfo()
{
$settings['domain'] = "mydomain";
$settings['pass'] = "1234";
return $settings;
}
}
class B extends A
{
function makeurl()
{
$ads = $this->getinfo();
return "template/" . $ads['domain'];
}
}
$theme=new B();
$mypass = $theme->getinfo();
echo $mypass['pass'];
What about making settings a public static variable in A? By making it a class variable you won't need an instance of A.
class A {
public static $settings;
// getter and setter methods here
}
// code elsewhere
echo A::$settings['pass'];
Also because your class B extends A it inherits the methods and properties, so you could call
$theme = new B();
$mySettings = $theme->GetInfo();
if B extends A, all protected and public members of A are inherited into B, so you can access them directly.
class A {
protected $foo;
public function __construct() { $this->foo = 1; }
}
class B extends A {
public function bar() {
echo $this->foo;
}
}
$b = B();
$b->bar();
If I understand you correctly, you're pretty close:
$theme=new B();
$settings = $theme->getinfo();
$mypass = $settings['pass'];
echo $mypass;
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);
Why does the following code print "1,1,1," instead of "4,5,6,"?
class MyClass {
// singleton instance
private static $instance = 3;
function __construct() {
$instance++;
echo $instance . ",";
}
}
for($i = 0; $i < 3; $i++) {
$obj = new MyClass();
}
$instance is a local variable, not a static class property. Unlike Java you always must access variables, or properties in theire scope
$var; // local variable
$this->var; // object property
self::$var; // class property
I just saw
// singleton instance
The singleton pattern is usually implemented different
class SingletonClass {
protected $instance = null;
protected $var = 3;
protected __construct () {}
protected __clone() {}
public static function getInstance () {
if (is_null(self::$instance)) { self::$instance = new self(); }
return self::$instance;
}
public function doSomething () {
$this->var++;
echo $this->var;
}
}
$a = SingletonClass::getInstance();
$a->doSomething();
The singleton pattern ensures, that you always interact with exactly one instance of a class.
In your constructor, $instance is not yet defined. You must use:
self::$instance++;
echo self::$instance . ",";
to reference the static property of your class.