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

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

Related

How to terminate last function in chaining method 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()

How to use a variable from a function in a class

I have a function declared in another file. I need to get a variable from it and use it in the class. But for some reason an error appears.
$myvar = myfunc($text);//an error here
class func{
public $title = $myvar;//and here
public function ($title){
...
}
}
You can inject the return value of the function in the constructor when the object is being initialized.
include_once 'func.php';
class Myclass{
public $title;
function __construct( $title = null )
{
$this->title = $title;
}
}
$obj = new Myclass( getVar() );
echo $obj->title;
func.php class
<?php
function getVar()
{
return 'this_var';
}
?>
Your question is about how to use a variable inside a class
class MyClass{
public $title; //do not put a variable here, this is invalid
///you could set a default like this
// e.g. public $title = "Jam";
//this is a setter
public function setTitle(string $title){
$this->title = $title
}
public function getTitle() {
return $this->title;
}
}
then use
$class = new MyClass();
$class->setTitle("jam sponge");
echo $class->getTitle();
or
define a constructor
class MyClass{
public $title;
//this is a constructor
public function __construct(string $title){
$this->title = $title
}
public function getTitle() {
return $this->title;
}
}
then use
$class = new MyClass("jam sponge");
echo $class->getTitle();
the latter is my preference.
As for this $myvar = myfunc($text);//an error here that's not relate to passing the var to the class. You have some other issue in that function, so either show that code or post anew question specific to it

Echo variable which has been set in another classes function

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 use method from other object /How pass right context?

class Person_Writer {
function writeName ( ){
echo $this ->name;
}
function writeAge ( ){
echo $this ->age;
}
}
class Person {
function __construct($name,$age) {
$this->writer = new Person_Writer;
$this->name= $name;
$this->age = $age;
}
function __call($name, $arguments) {
$writter = $this->writer;
call_user_func(array($this->writer, 'WriteName'));
// call_user_func(array(new Person_Writer, 'WriteName'));
}
}
$obj = new Person('sasha',28);
$obj->writeName();
Error :
Notice: Undefined property: Person_Writer::$name in
How use method from other object / How pass right context ?
I want to use function writeName ( ) in $obj .
I'm not quite sure what you're trying to do there, this would work if you want to call a function of another object:
class Person_Writer {
function writeName ($name){
echo $name;
}
function writeAge ($age){
echo $age;
}
}
class Person{
function __construct($name,$age) {
$this->writer = new Person_Writer;
$this->name= $name;
$this->age = $age;
}
function __call($name, $arguments) {
$writer = $this->writer;
$writer->writeName($this->name);
}
}
$obj = new Person('sasha',28);
$obj->writeName();
Why are you using $this->name in your Persone_Writer object ? This object won't know variables of the Person object that's why you got the undefined error.
EDIT: An other solution would be Hexana one where you extend objects.
You are not extending the base class and you have a typo. You are also not calling the writeAge() method. Below works for me:
class Person_Writer {
public function writeName ( ){
echo $this ->name;
}
function writeAge ( ){
echo $this ->age;
}
}
class Person extends Person_Writer{
function __construct($name,$age) {
$this->writer = new Person_Writer;
$this->name= $name;
$this->age = $age;
}
function __call($name, $arguments) {
$writer = $this->writer;
call_user_func(array($this->writer, 'WriteName'));
// call_user_func(array(new Person_Writer, 'WriteName'));
}
}
$obj = new Person('sasha',28);
$obj->writeName();
echo '<br>';
$obj->writeAge();

Php Class - How can I make a Class know parent value

<?php
class FirstClass{
public static $second;
public static $result = 'not this =/';
public function __construct(){
$this->result = 'ok';
$this->second = new SecondClass();
}
public function show(){
echo $this->second->value;
}
}
class SecondClass extends FirstClass{
public $value;
public function __construct(){
$this->value = parent::$result; //Make it get "ok" here
}
}
$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"
?>
How can I make it to print "ok"?
I mean, the SecondClass should know what FirstClass set as result, see?
Replace $this->result = 'ok'; with self::$result = 'ok'; in FirstClass constructor.
Btw, the code is terrible. You're mixing static and instance variables, and extend classes but don't use benefits extension provides.
you need to reference the static as self::$result in the first class.
Below should do what you want...
<?php
class FirstClass{
public static $second;
public static $result = 'not this =/';
public function __construct(){
self::$result = 'ok';
$this->second = new SecondClass();
}
public function show(){
echo $this->second->value;
}
}
class SecondClass extends FirstClass{
public $value;
public function __construct(){
$this->value = parent::$result; //Make it get "ok" here
}
}
$temp = new FirstClass();
$temp->show(); //It will show: "not this =/"
?>

Categories