How to terminate last function in chaining method php - php

I got some problem and I don't know how to fix it.
this is sample for the problem
class DancingClass {
private static $associate = [];
private static $first;
public static function first($param) {
self::$first = $param;
return new self;
}
public function second($param) {
self::$associate["second"] = $param;
return new self;
}
public function finish() {
var_dump(self::$associate["second"]);
$sec = self::$associate["second"] | "";
$all = self::$first . " ditemani oleh " . $sec;
return $all;
}
}
Then I call with chaining method
$callingClass = new DancingClass;
echo $callingClass::first("lucky")->second("adhitya")->finish(); // Return "lucky ditemani oleh adhitya"
echo "<br/>";
echo $callingClass::first("fatur")->finish(); // Return "fatur ditemani oleh"
but I got result like this

When you call second() method it sets variable on the same class instance that you call later.
Maybe you should try:
echo ((new DancingClass())->first(...)->second(...)->finish();
echo ((new DancingClass())->first(...)->finish()

Related

What do a reference of a method do?

What do this reference symbol (&) do in &methodName() method?
Is it necessary?
Is this called a reference by method?
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &methodName(){
return $this->value;
}
}
echo (new TestClass())->methodName(); //Outputs 5;
It means that the method returns a reference. If you then assign that to a reference variable outside the method, updating the variable will update the property.
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &refMethod(){
return $this->value;
}
public function valueMethod() {
return $this->value;
}
public function printValue() {
echo $this->value . "<br>";
}
}
$c = new TestClass();
$var1 = &$c->valueMethod();
$var1 = 10;
$c->printValue(); // prints 5
$var = &$c->refMethod();
$var = 20;
$c->printValue(); // prints 20

class __constructor don't return zerofill number

I have this class :
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct()
{
$this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
And I use it in this way :
$cod = new codici();
$cod_cliente = $cod->i = 1; //return 1
$cod_cliente = $cod->getPad(1); //return 0001
If I call the class direct, __constructor call internal method getPad and returns wrong answer '1'. Instead, if I call the method getPad return the correct value '0001'.
Why can't I use $cod_cliente=$cod->i=1 ?
$cod_cliente = $cod->i = 1;
It will set value for $cod_cliente and $cod->i both to 1. So when you print $cod_cliente, it will show 1.
But in case $cod_cliente = $cod->getPad(1), code to add padding executes and return 0001.
If you want your constructor to return something you should give it a parameter. And since your getPad($i) returns something you'd need to echo/print the results.
<?php
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $parameter;
echo $this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
This will allow you to call your class like this:
$c = new codici(3);
which would echo 0003.
this is right code:
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $this->getPad($parameter);
}
public function getPad($i)
{
return str_pad($i,4,'0',0);
}
}
now work:
$c= new codici(1);
echo $c->i;//return 0001
echo $c->getPad(1);//return 0001
thank a lot.

how pass parameter to php class by class()::function()

How pass parameter to PHP class by class()::function()?
class greenHouse{
public function __construct(connection $con){
}
public function show(){
}
}
$nameclass = 'greenHouse';
$namefunction = 'show';
$nameclass::$namefunction();
works
$nameclass = 'greenHouse';
$namefunction = 'show';
$nameclass($con)::$namefunction();
doesn't work
I want to pass a parameter to the class with $nameclass($con)::$namefunction();. How do I do that in PHP?
You are trying to call a function statically with that notation...
$nameclass = 'greenHouse';
$namefunction = 'show';
$class = new $nameclass($con);
$class->$namefunction();
You can instantiate an object and immediately discard it by calling new within braces:
class Test
{
private $name;
function __construct($name)
{
$this->name = $name;
}
function speak()
{
echo $this->name;
}
function __destruct()
{
echo 'dead';
}
}
$class='Test';
$method='speak';
(new $class('David'))->$method();
echo ' is ';
$temp = new $class('John');
$temp->$method();
echo ' is ';
//Daviddead is John is dead
So in your case:
(new $nameclass($con))->$namefunction();

Assign property value using an anonymous function

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

return data with chain static method

i'm trying to return data with chain static method , but i can't because the method return one thing only .
class Input
{
public static function set($input)
{
$data = $input;
$class = get_class();
return $data;
return self::$class = new $class;
}
public static function get()
{
echo ' - get method';
}
}
Input::set('ahmed')->get();
but it's only print " -get method "
I think you want
class Input
{
private static $data;
public static function set($input)
{
self::$data = $input;
return self;
}
public static function get()
{
echo self::$data.' - get method';
}
}
Input::set('ahmed')->get(); // ahmed - get method
but this you can use only once better is set name for value
class Input
{
private static $data = array();
public static function set($name, $input)
{
self::$data[$name] = $input;
return self;
}
public static function get($name)
{
echo self::$data[$name].' - get method';
}
}
Input::set('name', 'ahmed')->get('name'); // ahmed - get method

Categories