How can I call functions on the object after a outside class-instance call without using static classes.
Here is my sample (It should echo "OKAY!"):
class class1 {
function func1() {
func3(); // function outside class
}
function func2() {
echo "AY!";
}
}
$foo = new class1();
$foo->func1();
function func3()
{
echo "OK";
$foo->func2(); // class instance doesn't exist any more
}
class class1 {
function func1() {
func3($this); // function outside class
}
function func2() {
echo "AY!";
}
}
$foo = new class1();
$foo->func1();
function func3($object)
{
echo "OK";
$object->func2(); // class instance doesn't exist any more
}
Instance pass as argument. follow the code
<?php
class class1 {
function func1($foo) {
func3($foo); // function outside class
}
function func2() {
echo "AY!";
}
}
$foo = new class1();
$foo->func1($foo);
function func3($foo)
{
echo "OK";
$foo->func2(); // class instance doesn't exist any more
}
?>
Output:
OKAY!
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 do this?
class Foo
{
public function __construct($bar)
{
$barish = new $bar();
$barish->woo();
}
}
class Bar
{
function woo()
{
echo "wooooo";
die();
}
}
// Here the magic should happen
$foo = new Foo(Bar);
I expect wooooo but I get "Use of undefined constant Bar".
You are missing '':
class Foo
{
public function __construct($bar)
{
$barish = new $bar();
$barish->woo();
}
}
class Bar
{
function woo()
{
echo "wooooo";
die();
}
}
// Here the magic should happen
$foo = new Foo('Bar');
I'm trying to pass a variable to a method in an extended class, but it's not working.
Here's the sample code:
class set2 extends set1
{
function Body($variable) {
}
}
$start2 = new set2();
$start2->Body('some text');
The last line is the part I'm trying to get to work. I'm not sure if I should have a constructor instead to do it or how it's best to get it to work.
I figured it out. I just added a public variable instead and passed its value like this:
class set2 extends set1
{
public $variable = NULL;
function Body() {
echo $this->variable;
}
}
$start2 = new set2();
$start2->variable = 'Some Text';
Three different ways of doing what I think you're trying to do:
class set1
{
protected $headVariable;
function Head() {
echo $this->headVariable;
}
function Body($variable) {
echo $variable;
}
function Foot() {
echo static::$footVariable;
}
}
class set2 extends set1
{
protected static $footVariable;
function Head($variable) {
$this->headVariable = $variable;
parent::Head();
}
function Body($variable) {
parent::Body($variable);
}
function Foot($variable) {
self::$footVariable = $variable;
parent::Foot();
}
}
$start2 = new set2();
$start2->Head('some text');
$start2->Body('some more text');
$start2->Foot('yet more text');
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();
}
...
}
Is any way to create class static var inside method?
something like this..
class foo {
public function bind($name, $value) {
self::$name = $value;
}
};
or is there other solution to bind variables to class and later use it without long and ugly syntax "$this->"
I'm not sure I understand the question. But if you'd like to attach variables at runtime, you could do this:
abstract class RuntimeVariableBinder
{
protected $__dict__ = array();
protected function __get($name) {
if (isset($this->__dict__[$name])) {
return $this->__dict__[$name];
} else {
return null;
}
}
protected function __set($name, $value) {
$this->__dict__[$name] = $value;
}
}
class Foo
extends RuntimeVariableBinder
{
// Explicitly allow calling code to get/set variables
public function __get($name) {
return parent::__get($name);
}
public function __set($name, $value) {
parent::__set($name, $value);
}
}
$foo = new Foo();
$foo->bar = "Hello, world!";
echo $foo->bar; // Prints "Hello, world!"
http://codepad.org/H9bz2uVp
Using self would result in a fatal error, as the property is undeclared. You would have to use $this which would then be accessible as a public variable:
<?php
class foo {
public function bind($name, $value) {
$this->$name = $value;
}
}
$foo = new Foo;
$foo->bind('bar','Hello World');
echo '<pre>';
print_r($foo);
echo $foo->bar;
echo '</pre>';?>