how to call a function inside a class using php [duplicate] - php

This question already has answers here:
Calling a function within a Class method?
(10 answers)
Closed 9 years ago.
please help me with this example.. I want to call the function inside the class..
FUNCTION
class myTest(){
function Me(){
$x = 2;
}
}
is this the answer?
$myval = new myTest();
echo $myval;
thank you

You need to first instantiate a new object of type 'myTest' like so:
$myObj = new myTest;
And then you can use the function like so:
$myObj->Me();
Note that at present, the function returns nothing so you'll get a 'blank screen'.
If you changed your function to read:
return "Hello";
Then you would get 'Hello' printed on screen by using the above.
I hope that helps?

$myval = new myTest(); here $myval is object of the your Class .
You can call the inside function with just $myval->Me();

Related

How to assign a variable outside php function [duplicate]

This question already has answers here:
Are PHP Variables passed by value or by reference?
(16 answers)
How to declare a global variable in php?
(10 answers)
Closed 3 years ago.
I would like to be able to assign the name of a variable outside the function so that the function can assign the chosen variable. It does not seem to work. Anyone have any ideas?
Below is my attempt, however the $admin variable is empty after running the function.
function assign_check($variable, $check) {
if (empty($_POST[$check])) {
$variable = "no";
} else {
$variable = $_POST[$check];
}
}
assign_check('$admin', 'admin');
My question is not about the use of global variables.
You can request a reference in the function body.
function assign_check(&$variable, $check) {
$variable = 'hello';
}
And call passing a variable (reference).
assign_check($admin, 'admin');
$admin value is now 'hello';
Fitting that to your code would result in
function assign_check(&$variable, $check) {
$variable = empty($_POST[$check]) ? "no" : $_POST[$check];
}
assign_check($admin', 'admin');
But returning a proper value would be much cleaner code than using references. Using a ternary operator like presented above would it even simplify without need of a function at all.
A normal way to assign the result of a function to a variable name specified outside the function would be to have the function return the result and assign it directly to the variable when you call the function.
function assign_check($check) {
if (empty($_POST[$check])) {
return "no";
} else {
return $_POST[$check];
}
}
$admin = assign_check('admin');
I would do it this way unless there was a compelling reason to do it otherwise.
For the specific type of thing it looks like this function is intended to do, I would suggest looking at filter_input.

how to check if object in array exist? [PHP] [duplicate]

This question already has answers here:
Check if a function exists in a class before calling call_user_func()
(4 answers)
Closed 6 years ago.
I receive a response from API in form of array object, but sometimes i receive different data in form of object data
for example :
//first response
$response->getBody();
//second response
$response->getMessage();
so if i call one of the response above that doesnt have the object , it's gonna have an error, what i've done so far is like this:
if(empty($response->getBody())){
//do something
}
in conclusion i want to detect if the array of object has the object i want to call or use
use method_exists() to check if the method of a class exits.
<?php
$directory = new Directory('.');
var_dump(method_exists($directory,'read'));
?>
or use is_callable()
class someClass {
function someMethod() { }
}
$anObject = new someClass();
$methodVariable = array($anObject, 'someMethod');
var_dump(is_callable($methodVariable, true, $callable_name)); //
bool(true)
echo $callable_name, "\n"; // someClass::someMethod

How to call PHP function from string stored in a Variable with arguments [duplicate]

This question already has answers here:
How to call a function from a string stored in a variable?
(18 answers)
Closed 1 year ago.
I found question from here. But I need to call function name with argument.
I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:
function foo ($argument)
{
//code here
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$functionName($argument);//Call here foo function with argument
// i need to call the function based on what is $functionName
Anyhelp would be appreciate.
Wow one doesn't expect such a question from a user with 4 golds. Your code already works
<?php
function foo ($argument)
{
echo $argument;
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$argument="Joke";
$functionName($argument); // works already, might as well have tried :)
?>
Output
Joke
Fiddle
Now on to a bit of theory, such functions are called Variable Functions
PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.
If you want to call a function dynamically with argument then you can try like this :
function foo ($argument)
{
//code here
}
call_user_func('foo', "argument"); // php library funtion
Hope it helps you.
you can use the php function call_user_func.
function foo($argument)
{
echo $argument;
}
$functionName = "foo";
$argument = "bar";
call_user_func($functionName, $argument);
if you are in a class, you can use call_user_func_array:
//pass as first parameter an array with the object, in this case the class itself ($this) and the function name
call_user_func_array(array($this, $functionName), array($argument1, $argument2));

php single function with multiple names / aliases [duplicate]

This question already has answers here:
How to alias a function in PHP?
(15 answers)
Closed 8 years ago.
This isn't really important but the question is more one of curiosity.
Is it possible to alias a function or define two names for it.
I know this works:
function real($p1=array(), $p2=null, $p3='default'){
return 'something';
}
function aliasForReal($p1=array(), $p2=null, $p3='default'){
return real($p1, $p2, $p3);
}
Is there a less verbose way to alias another function?
something like
function (real||aliasForReal)(...){
or
function aliasForReal extends real;
There are a couple of places I need to do this and the working method above just feels a bit dirty to me.
For instance:
using names like (begin and start) interchangeably for one function and (end and stop) for another.
function real($p1=array(), $p2=null, $p3='default'){
return 'something';
}
$real1 = 'real';
$real2 = 'real';
// etc
You can call $real1(...)
function real(){
return "real";
}
function realAlias(){
return "realAlias";
}
$p = "real";
print $p();
$p = "realAlias";
print $p();
does it help?

How to use the -> in PHP [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does this php construct mean: $html->redirect(“URL”)?
Hi, I've been looking for what this operator "->" does, but I can't seem to find a reference to it, only people using it in come and I can't make out what its doing, an explanation would be appreciated.
The -> is used with object methods/properties, example:
class foo{
function bar(){
echo 'Hello World';
}
}
$obj = new foo;
$obj->bar(); // Hello World
More Info:
http://de.php.net/manual/en/language.oop5.basic.php
http://php.net/manual/en/tokens.php
It's for classes.
See here:
http://de.php.net/manual/en/language.oop5.basic.php
-> operator access properties and methods of an object.
Probably you should read PHP OOP introduction: http://php.net/manual/en/language.oop5.php
Expanding on sarfraz's answer to demonstrate how to access properties:
class foo{
public $value = "test";
function bar(){
//// code
}
}
$obj = new foo;
$obj->bar();
echo $obj->value; //displays "test"

Categories