I have a question close to this:
How to create constructor with optional parameters?
It is clear that we can make optional params like this:
function myFunction($param='hello')
but I want to use it in a class's method with $this->something instead of 'hello', it looks like this:
public function myFunction($param=$this->property)
but I get a:
Parse error: syntax error, unexpected '$this'
Is it possible to get it?
you need to set to null and check afterward
class example {
private $something = "something";
public function myFunction($a = null)
{
if($a === null)
$a = $this->something;
// more code goes here
return $a;
}
}
$test = new example();
print $test->myFunction();
// prints "something"
print $test->myFunction("hello");
// prints "hello"
You can do it like this
$Obj = new myclass();
class myclass{
var $data = 'tested';
function __construct(){
$this->testFunction();
}
public function testFunction($param = null){
if( $param == '' || $param == null){
$param = $this->data;
}
echo $param;
}
}
Related
I don't know what is this call system.
For example:
$a = SameClass::fnc1()->fnc2('input')->fnc2();
and we can see this method in jQuery:
$("#selector").parent().css('left',5).fadeOut(1500);
I don't have any idea to write this codes structure in PHP.
You just need to return object from a function
For example
class First
{
public function funcFirst()
{
$obj = new Second;
return $obj;
}
}
class Second
{
public function funcSecond($message)
{
return $message;
}
}
$first = new First;
$message = "Hello";
$result = $first->funcFirst()->funcSecond($message); // 'Hello'
When generating an object this way AND executing a method, PHP gives an error.
class A {
static public function b() {
$o = new get_called_class(); // works
$class = get_called_class();
$o = new $class; // works
$o = (new $class)->method(); // works
$o = (new get_called_class())->method(); // doesn't work
// error message: Class '...\get_called_class' not found
$o = (new (get_called_class()))->method(); // doesn't work
// error message: syntax error, unexpected '('
}
}
Why does the last lines fail?
How to write it in one line?
Unfortunately you can't do it directly with the function's return value, but you can save it into a variables and use the variable. You can also use static or self constants.
$class = get_called_class();
$o = (new $class())->method();
$o = (new static())->method();
$o = (new self())->method();
Not possible at all. If you want to use an instance, store it in a variable.
class MyClass {
public function method(): string {
return "Hello World";
}
}
$instance = new MyClass();
$result = $instance->method();
You could work around if you do not need an instance by using a static method.
class MyClass {
public static function method(): string {
return "Hello World";
}
}
$result = MyClass::method();
My class is like this:
<?php
class ExampleClass{
private $example_property = false;
public function __construct(){
$this->example_property = function() {
$this->example_property = 1;
return $this->example_property;
};
}
public function get_example_property(){
return $this->example_property;
}
}
$example = new ExampleClass();
echo $example->get_example_property();
Property $example_property must be false until you call it, then, the first time it is called, I want to assign 1 to it. What's wrong with my code?
Error: Error Object of class Closure could not be converted to string on line number 20.
I just tried to play a little bit with your code.
To make it possible, you'll have to find out, whether your variable is a function (defined in your __construct) or the number set by this function
<?php
class ExampleClass{
private $example_property = false;
public function __construct(){
$this->example_property = function() {
$this->example_property = 1;
return $this->example_property;
};
}
public function get_example_property(){
$func = $this->example_property;
if(is_object($func) && get_class($func)=='Closure'){
return $func();
}
return $func;
}
}
$example = new ExampleClass();
echo $example->get_example_property(); //returning 1
echo $example->get_example_property(); //returning 1 again
But anyway, I don't see any sense in doing this.
The typical solution would be something like this:
class ExampleClass{
private $example_property = false;
public function __construct(){
//usually initializing properties goes here.
//$this->example_property = 1;
}
public function get_example_property(){
// I think you want a value to be initialzed only if needed.
// so then it can go here.
if(!$this->example_property) {
$this->example_property = 1; //initialize it, if needed
}
return $this->example_property;
}
}
$example = new ExampleClass();
echo $example->get_example_property(); //returning 1
echo $example->get_example_property(); //returning 1 again
Question:
How to push $param from load() to $data property in class A?
Therefor I can use get_class_vars get_object_vars to load them.
Each time I use load function, it will add $param to $data property.
Example:
<?php
class Test {
public function testing($str) { echo $str; }
}
class A {
public $data = array();
public function load($param) {
array_push($this->data, $param); // not adding $param to $data property
return $param = new $param;
}
}
class B {
public $a;
public function __construct() {
$this->a = new A();
var_dump(get_object_vars($this->a)); // showing empty $data property
}
}
// Usage
$b = new B();
$test = $b->a->load('test');
$test->testing('hello');
Edit:
used get_object_vars($this->a)
get_class_vars only shows default public variables. Use get_object_vars($this->a) instead; this should work.
is there a better way to call an anonymous function inside a class? Here is a simple example that clearifies what I mean.
class foo
{
private $call_back_1 = null;
private $call_back_2 = null;
function __construct($func1, $func2)
{
$this->call_back_1 = func1;
$this->call_back_2 = func2;
}
function provokeCallBacks()
{
//this does not work, and gives an error
$this->call_back_1();
//I would like to avoid this
$method = $this->call_back_2;
$method();
}
}
$call1 = function(){ echo "inside call 1"};
$call2 = function(){ echo "inside call 2"};
$test = new foo(call1, call2);
$test->provokeCallBacks();
* Update 1: Please ignore any syntax error as I have written this on the fly for demo puposes. *
Inside foo:provokeCallBacks, I am trying to call the anonymous functions how ever the first way does not works and gives an error. The second one works but it's a bit stupid that I have to use a temp variable called "$method" to make the call.
I want to know if there exists a better way to call the anonymous function.
call_user_func($this->call_back_1);
No, it's not possible to call an anonymous function via $this.
Another options is;
call_user_func($this->call_back_1);
Being PHP loosely typed, it can't do like {$this -> callback}(); you have to store it in a temp variable or to use call_user_func() either.
EDIT - consider something like this:
class Lambdas
{
protected $double;
protected $triple;
public function __construct($double, $triple)
{
$this -> double = $double;
$this -> triple = $triple;
}
public function __call($name, $arguments)
{
if( is_callable($this -> {$name}) ){
return call_user_func_array($this -> {$name}, $arguments);
}
}
}
$lambdas = new Lambdas(
function($a){ return $a * 2;},
function($a){ return $a * 3;}
);
echo $lambdas -> double(2); // prints 4
echo $lambdas -> triple(2); // prints 6
Dirty and dangerous, but you might succeed using eval..
class foo
{
private $call_back_1 = null;
private $call_back_2 = null;
function __construct($func1, $func2)
{
$this->call_back_1 = func1;
$this->call_back_2 = func2;
}
function provokeCallBacks()
{
eval($this->call_back_1);
eval($this->call_back_2);
}
}
call1 = 'echo "inside call 1"';
call2 = 'echo "inside call 2"';
$test = foo(call1, call2);
$test->provokeCallBacks();
I know your question has been answered but you can try changing your approch ..
class Foo {
private $calls = array();
function __set($key, $value) {
$this->calls[$key] = $value;
}
function __call($name, $arg) {
if (array_key_exists($name, $this->calls)) {
$this->calls[$name]();
}
}
function __all() {
foreach ( $this->calls as $call ) {
$call();
}
}
}
$test = new Foo();
$test->A = function () {
echo "inside call 1";
};
$test->B = function () {
echo "inside call 2";
};
$test->A(); // inside call 1
$test->B(); // inside call 2
$test->__all(); // inside call 1 & inside call 2