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);
}
}
Related
class A
{
public function child1()
{
$var1 = 'abc';
}
}
class B extends A
{
public function child1()
{
echo parent::$var1; // Return error undefined class constant 'var1'
}
}
How can i access $var1 in this situation ?
Expected result: 'abc'
First you cannot do class B extends class A. The correct syntax would be class B extends A:
class A
{
public function child1()
{
$var1 = 'abc';
return $var1;
}
}
class B extends A
{
public function child1()
{
echo parent::child1();
}
}
$temp = new B;
$temp->child1();
Now what I've done is return the $var1 in your class A.
You cannot call echo parent::$var1; because it is inside a function, so you call the parent function echo parent::child1();.
working example here
You need to make $var1 a class property. See following code:
<?php
class A
{
protected $var1;
public function child1()
{
$this->var1 = 'abc';
}
}
class B extends A
{
public function child1()
{
parent::child1();
echo $this->var1;
}
}
$b = new B();
$b->child1();
?>
I have 3 classes:
Class A - Parent Class
Class B - Child Class
Class C - Class to be used in Class A
I want to use functions from class C using variables from my Child class.
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$class->test = $this->test;
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->test = 1;
$b->c->display();
Your problem is here:
$class->test = $this->test;
You are attempting to use a property that is not yet defined, because when you do this:
$b->test = 1;
the constructor has already been called, and there's nothing in your classes to update C with the value of B's test property.
You can solve this in a couple of different ways.
1) Send the value in B's constructor, and pass it down the entire chain:
class A
{
public function __construct($test)
{
$this->load($test);
}
public function load($test)
{
$class = new C();
$class->test = $test;
$this->c = $class;
}
}
class B extends A
{
public function __construct($test)
{
parent::__construct($test);
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B(123);
$b->c->display();
2) Add a method to B that will update C's property:
<?php
class A
{
public function __construct()
{
$this->load();
}
public function load()
{
$class = new C();
$this->c = $class;
}
}
class B extends A
{
public function __construct()
{
parent::__construct();
}
public function setTest($test)
{
$this->c->test = $test;
}
}
class C
{
public function display()
{
echo $this->test;
}
}
$b = new B();
$b->setTest(123);
$b->c->display();
Or perhaps a combination of both.
<?php
class A {
private function foo() {
echo "baf!<br/>";
}
public function test() {
$this->foo();
}
}
class C extends A {
private function foo() {
echo "bar!<br/>";
}
}
$c = new C();
$c->test(); //Prints baf!
?>
How to make c->test() to print bar! ? I was expecting that foo() will be overridden in C and would print bar! can someone explain it to me?
By making both methods protected you will achieve the desired result; the reason being that within A::test() it can only resolve $this->foo() to A::foo() because of the private visibility. See Visibility.
class A {
protected function foo() {
echo "baf!<br/>";
}
public function test() {
$this->foo();
}
}
class C extends A {
protected function foo() {
echo "bar!<br/>";
}
}
$c = new C();
$c->test(); //Prints bar!
class A {
protected function foo() {
echo "baf!<br/>";
}
public function test() {
$this->foo();
}
}
class C extends A {
protected function foo() {
echo "bar!<br/>";
}
}
$c = new C();
$c->test(); //Prints baf!
just make both the foo function as public or protected access level.
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 need to know what kind invokes a static method, without sending as parameter
class foo
{
public static function test($clase)
{
echo "Class invoke:" . FUNCTION();
}
}
class A { public function x { foo::test(); } }
class B { public function y { foo::test(); } }
class C { public function z { foo::test(); } }
You can use late static bindings and get_called_class() (PHP >= 5.3) if you make all of your classes extend foo, like this:
class foo
{
public static function test($clase)
{
echo "Class invoke:" . get_called_class();
}
}
class A extends foo { public function x() { self::test(''); } }
class B extends foo { public function y() { self::test(''); } }
class C extends foo { public function z() { self::test(''); } }
With these objects:
$a = new A; $a->x();
$b = new B; $b->y();
$c = new C; $c->z();
You'll get as output:
Class invoke:A
Class invoke:B
Class invoke:C