PHP Call Class Method/Function

PHP call Class method / function

To answer your question, the current method would be to create the object then call the method:

$functions = new Functions();
$var = $functions->filter($_GET['params']);

Another way would be to make the method static since the class has no private data to rely on:

public static function filter($data){

This can then be called like so:

$var = Functions::filter($_GET['params']);

Lastly, you do not need a class and can just have a file of functions which you include. So you remove the class Functions and the public in the method. This can then be called like you tried:

$var = filter($_GET['params']);

php call class function by string name

The callback syntax is a little odd in PHP. What you need to do is make an array. The 1st element is the object, and the 2nd is the method.

call_user_func(array($player, 'SayHi'));

You can also do it without call_user_func:

$player->{'SayHi'}();

Or:

$method = 'SayHi';
$player->$method();

PHP: Calling another class' method

//file1.php
<?php

class ClassA
{
private $name = 'John';

function getName()
{
return $this->name;
}
}
?>

//file2.php
<?php
include ("file1.php");

class ClassB
{

function __construct()
{
}

function callA()
{
$classA = new ClassA();
$name = $classA->getName();
echo $name; //Prints John
}
}

$classb = new ClassB();
$classb->callA();
?>

How to call a function or method inside its own class in php?

You can use class functions using $this

<?php
class A{

function b($msg){
return $msg;
}

function c(){
$m = $this->b('Mesage');
echo $m;
}
}

Call php class method from string with parameter

When you want to call a method on an object with call_user_func() you have to pass it an array with the first element as the object or class name that the method will be called on and the second element being the name of the method, e.g.:

$tags_array = call_user_func( array($this,'get_images_for_tag'), $row["id"]);

Calling method from another class in php

Consider following example of Basic OOP concept in PHP

There are 2 files in a web accessible directory like in htdocs if using XAMPP

One.php

<?php
// One.php

/**
*
*/
class One
{
public $one_p;
// function __construct(argument)
// {
// # code...
// }

public function test_two()
{
var_dump($this->one_p);
}
}

Two.php

<?php
// Two.php
require 'One.php';
/**
*
*/
class Two
{

// function __construct(argument)
// {
// # code...
// }

public function test_one()
{
$one_obj = new One;
$one_obj->one_p = 'hello prop';
$one_obj->test_two();
}
}

$two_obj = new Two;
$two_obj->test_one();

Now run Two.php in browser and observe the result, it is
string(10) "hello prop"

Now comment $one_obj->one_p = 'hello prop'; line and observe result, it is NULL

So we can conclude that once a property (a variable) is set, it is globally accessible.
This is the concept of getters and setters in PHP OOP. Please refer here. You don't need to pass it in argument like you need in function

In Yii example

$one_obj->one_p = 'hello prop';

is like below

$one_obj->one_p = new Someclass;  // here Someclass should be 'require' at the begining of file

so here you can access all properties and method of Someclass like
$one_obj->one_p->somemethod();

Since getInstance() is static method of UploadFile, you can call it without creating object.

And getInstance() returns an object
You can store anything you need to store in one_p like int, float, array, resource, object ...
Hope you got it.

Yii is a very fine framework of PHP completely coded in OOP style, not coded in procedural style, engaging MVC architecture. You will enjoy it more, just go through here

How to call class methods that do not exist in php?

You can use the magic method __call to solved your situation

<?php
class Called
{
private $list = array('Id' => 1, 'Name' => 'Vivek Aasaithambi');

public function __call($name, $arguments) {
$field = substr($name, 3);
echo $this->list[$field];
}
}

$obj = new Called();
$obj->getId();
echo "<br/>\n";
$obj->getName();

?>

You can read more about __call in:

http://php.net/manual/en/language.oop5.overloading.php#object.call

Call method from another class in a new class - PHP

For this I'd use dependency injection. Which is just a fancy way of saying "sending an object of the A class when creating B".

In other words, something like this:

class typeA {
public function __construct () {};
public function test () {
return 'Test string';
}
}

class typeB {
protected $testObj;

public function __construct (typeA $testCase)  {
$this->testObj = $testCase;
}

public function getTest () {
return $this->testObj->test ();
}
}

$a = new typeA ();
$b = new typeB ($a);
echo $b->getTest ();

Constructors are meant to be used to create an object that's ready to be used, which is why I've just stored the dependency inside the typeB object itself. Then, in the getTest() method I invoke the test() method of the object I'm depending upon, in order to get the needed data from it.

Doing it in this manner will allow you to write flexible OOP code, which can easily be expanded and extended as you require. Hiding the dependencies inside the constructors, by creating objects there, creates a hidden and hard dependency. Something which makes it a lot harder, if not down right impossible, to properly leverage the extensible nature of the class-based designs.



Related Topics



Leave a reply



Submit