PHP class as argument - php

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');

Related

Pass variables from class instance to its extended method

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');

PHP calling method from class into a child of another class

please help me here.
I have a class:
class Foo() {
public function{
if($var = x){
do this;
}
else {
do that;
}
}
}
and another class:
class B extends A() {
public function {
#need to import method from Foo
#to execute on a varible in this class
}
}
Could someone please help me out on how to go about this. The language is PHP
class Foo {
protected $var;
function __construct($var) {
$this->var = $var;
}
function test() {
echo "Method Test from class Foo<br>";
if ($this->var == NULL) {
echo "Test = = Null <br>";
}
else {
echo "Test != = Null <br>";
}
}
}
class Ftt extends Foo {
protected $var1;
function __construct($var, $var1) {
$this->var1 = $var1;
parent::__construct($var);
}
function test() {
parent::test();
echo "Method Test from class Ftt extends Foo";
echo " with $this->var1 <br>";
}
}
$ftt = new Ftt('notnull', 'var1');
$ftt->test('notnull');
$foo = new Foo('');
$foo->test();
class Foo() {
public static function test {
if($var = x) {
do this;
}
else {
do that;
}
}
}
class B extends A() {
private $variable = 2;
public function test {
Foo::test($this->variable);
}
}

Get object after outside function call

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!

Is it possible to use functions from a "false parent" class, that have the current class as a property?

The following example defines a foo class, which constructs a bar class, and stores it as a property $foo->bar. In the bar class, is it possible to reference the 'false' parent class, and use it's functions?
class bar
{
public function test_false_parent()
{
//Is it possible to access foo->display() from here
{unknown code}::display();
}
}
class foo
{
public $bar;
public function __construct()
{
$this->bar = new bar;
}
public function display()
{
echo "in";
}
}
$foo = new foo;
$foo->bar->test_false_parent();
//Equivalent to $foo->display();
Not without a back reference:
class bar
{
protected $foo;
public function __construct(foo $foo)
{
$this->foo = $foo;
}
public function test_false_parent()
{
$this->foo->display();
}
}
class foo
{
public $bar;
public function __construct()
{
$this->bar = new bar($this);
}
public function display()
{
echo "in";
}
}
$foo = new foo;
$foo->bar->test_false_parent();

PHP Create class variables

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>';?>

Categories