Example:
Class Test {
private function __construct() {}
public static function init() {
$new_test = new Test();
return $new_test->inner_test;
}
public function inner_test() {
print '!!!!';
}
}
$test = Test::init();
$test();
Returns "PHP Fatal error: Function name must be a string"
Is there a way to accomplish this Javascript type behavior in PHP?
Below PHP 5.4
Class Test {
private function __construct() {}
public static function init() {
$new_test = new Test();
return $new_test->inner_test();
}
public function inner_test() {
print '!!!!';
}
}
$test = array('Test','init');
call_user_func($test);
For Phil:
function test(){
echo "Hello";
}
$test = test;
$test();
Run that in the sandbox.
Thanks to Phil for the link. This works:
Class Test {
private function __construct($myString) {
$this->myString = $myString;
}
public static function init($myString) {
$new_test = new Test($myString);
return function() use (&$new_test) {$new_test->inner_test();};
}
public function inner_test() {
print $this->myString;
}
}
$test = Test::init('Lorem Ipsum Dolar');
$test();
Related
I am trying to access the contents of a variable from another class. I have the code below, I am expecting to get 'test' returned, I get nothing.
I assume this is because it is getting $abc_rank as empty. It is required that the variable is populated in the function itself.
Therefore how can I get $abc_rank to hold that echo and output via the other class?
<?php
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
$this->abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
$go = new class2();
?>
I know I can do:
public static $abc_rank = 'test';
But the population of the variable must be in a function.
I have read some of the other related answers and can't seem to get this to work.
In class1 :
Replace $this->abc_rank = 'test'; with $this::$abc_rank='test';
($abc_rank is a static property)
In class2 :
In your display function : replace
$test = class1::$abc_rank;
echo $test;
with
$test = new class1();
echo $test::$abc_rank;
(class1 isn't static)
Full code here :
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
//$this->abc_rank = 'test';
$this::$abc_rank='test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
//$test = class1::$abc_rank;
//echo $test;
$test = new class1();
echo $test::$abc_rank;
}
}
$go = new class2();
you have to create the class1 to run the constructor of this class.
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
self::$abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
new class1();
$go = new class2();
How do I make chained objects in PHP5 classes? Examples:
$myclass->foo->bar->baz();
$this->foo->bar->baz();
Not: $myclass->foo()->bar()->baz();
See also:http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html
actually this questions is ambiguous.... for me this #Geo's answer is right one.
What you (#Anti) says could be composition
This is my example for this:
<?php
class Greeting {
private $what;
private $who;
public function say($what) {
$this->what = $what;
return $this;
}
public function to($who) {
$this->who = $who;
return $this;
}
public function __toString() {
return sprintf("%s %s\n", $this->what, $this->who);
}
}
$greeting = new Greeting();
echo $greeting->say('hola')->to('gabriel'); // will print: hola gabriel
?>
As long as your $myclass has a member/property that is an instance itself it will work just like that.
class foo {
public $bar;
}
class bar {
public function hello() {
return "hello world";
}
}
$myclass = new foo();
$myclass->bar = new bar();
print $myclass->bar->hello();
In order to chain function calls like that, usually you return self ( or this ) from the function.
Is there a way to get rid of the getCopy() method in the derived class HelloAndGoodbye, given that it looks the same as the getCopy() in the base class Hello?
And what is more, what is there an efficient way to achieve this?
(the only difference between the two functions is that in the baseclass 'static' refers to 'Hello' and in the derived class 'static' refers to 'HelloAndGoodbye; as for the variables contained therein they can be easily renamed so that they are the same in both functions).
<?php
class Hello {
private $hello;
public function createStub() {
return new static(null);
}
public function __construct($hello) {
$this->setHello($hello);
}
public function getCopy() {
$helloCopy = static::createStub();
$this->doCopy($helloCopy);
return $helloCopy;
}
public function doCopy($helloCopy) {
$helloCopy->setHello($this->hello);
}
public function setHello($hello) {
$this->hello = $hello;
}
public function getHello($hello) {
return $this->hello;
}
public function __toString() {
return $this->hello . "\n";
}
}
class HelloAndGoodbye extends Hello {
private $goodbye;
public function createStub() {
return new static(null, null);
}
public function __construct($hello, $goodbye) {
parent::__construct($hello);
$this->setGoodbye($goodbye);
}
public function getCopy() {
$helloAndGoodbyeCopy = static::createStub();
$this->doCopy($helloAndGoodbyeCopy);
return $helloAndGoodbyeCopy;
}
public function doCopy($helloAndGoodbyeCopy) {
parent::doCopy($helloAndGoodbyeCopy);
$helloAndGoodbyeCopy->setGoodbye($this->goodbye);
}
public function setGoodbye($goodbye) {
$this->goodbye = $goodbye;
}
public function getGoodbye($goodbye) {
return $this->goodbye;
}
public function __toString() {
return parent::__toString() . $this->goodbye . "\n";
}
}
function test() {
$hello = new Hello("Hello John");
$helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");
echo $hello;
echo $helloAndGoodbye;
}
test();
OUTPUT:
Hello John
Hello Jane
Goodbye Jane
I found a solution to the problem at hand by means of using the __CLASS__ PHP constant which correspond to the name of the class within which it appears. This allowed me to get rid of the pseudo-duplicate getCopy() method in the derived class, while still allowing getCopy() to work fine on both:
<?php
class Hello {
private $hello;
public function createStub() {
return new static(null);
}
public function __construct($hello) {
$this->setHello($hello);
}
public function getCopy() {
$class = __CLASS;
$instanceCopy = $class::createStub();
$this->doCopy($instanceCopy);
return $instanceCopy;
}
public function doCopy($helloCopy) {
$helloCopy->setHello($this->hello);
}
public function setHello($hello) {
$this->hello = $hello;
}
public function getHello($hello) {
return $this->hello;
}
public function __toString() {
return $this->hello . "\n";
}
}
class HelloAndGoodbye extends Hello {
private $goodbye;
public function createStub() {
return new static(null, null);
}
public function __construct($hello, $goodbye) {
parent::__construct($hello);
$this->setGoodbye($goodbye);
}
public function doCopy($helloAndGoodbyeCopy) {
parent::doCopy($helloAndGoodbyeCopy);
$helloAndGoodbyeCopy->setGoodbye($this->goodbye);
}
public function setGoodbye($goodbye) {
$this->goodbye = $goodbye;
}
public function getGoodbye($goodbye) {
return $this->goodbye;
}
public function __toString() {
return parent::__toString() . $this->goodbye . "\n";
}
}
function test() {
$hello = new Hello("Hello John");
$helloAndGoodbye = new HelloAndGoodbye("Hello Jane", "Goodbye Jane");
echo $hello;
echo $helloAndGoodbye;
}
test();
OUTPUT:
Hello John
Hello Jane
Goodbye Jane
<?php
class MyClass {
private $init;
public function __construct () {
$this->init = "Hello World";
}
}
?>
Hi guys, need your help.
If I have a private variable $init in MyClass, how would I write a method which allows $init to be modifiable after instantiation?
You can use setters and getters.
class MyClass {
private $init;
public function __construct () {
$this->init = "Hello World";
}
public function getInit(){
return $this->init;
}
public function setInit($init){
$this->init = $init;
}
}
How to use:
$myClass = new MyClass;
$myClass->setInit("New Init");
echo $myClass->getInit();
I have a class and two functions inside it as follows:
class MyClassName
{
protected function myFunction1()
{
// some code here
return $something;
}
public function myFunction2()
{
// some code here
return $somethingElse;
}
}
What I need to do is define a variable in myFunction1() and then use it in myFunction2(). What is the best practice to do that?
class MyClassName
{
public $var = 0;
protected function myFunction1()
{
// some code here
$this->var = ...;
return $something;
}
public function myFunction2()
{
// some code here
echo $this->var;
return $somethingElse;
}
}
Actually vars should be defined out of the function and then set a value. Then can be modified over all the script, by doing this->var
Make it a class property
class MyClassName
{
private $property;
public function __construct() {
$this->myFunction1();
}
protected function myFunction1()
{
// some code here
$this->property = 'an apple';
}
public function myFunction2()
{
// some code here
return $this->property;
}
}
Now test it:
$my_class = new MyClassName();
$something = $my_class->myFunction2();
echo $something;