Function is not returning the value - php

I am calling a function test() in my controller and test() function checks for submit button pressed. if submit button pressed, then it will call a model function. the model function should return a value but it does not. In my controller file, it is supposed to echo test(); but it does not.
Here is my code:
controller file:
<?php
class Controller {
include "model.php";
private $model;
public function __construct() {
$this->model = new Model();
echo test();
}
public function test() {
if($_POST['submit']) {
$this->model->returnThisValue();
}
}
}
?>
model file:
<?php
class Model {
public function returnThisValue() {
return "hello";
}
}
?>

Change your function to
public function test() {
if($_POST['submit']) {
return $this->model->returnThisValue();
}
}
This will return the value to the calling method and allow you to do something with it, like echo it to the output.

Related

function is not getting updated value of variable from another function in codeigniter

The value of variable $jwtoken in method addNew() is null. I want to pass auth() method value "some val" to addNew() method/function.
class Employees extends CI_Controller
{
public $jwtoken = null;
public function __construct()
{
parent::__construct();
}
public function auth() {
if(condition is true){
$this->jwtoken = 'val1';
}
else{
$this->jwtoken = 'val2';
}
}
public function addNew()
{
$this->auth();
echo $this->jwtoken;
}
// ..
}
Try this..
class Employees extends CI_Controller{
public function __construct()
{
parent::__construct();
}
public function auth() {
if(condition==true){
return "some val";
}else{
return "some other val";
}
}
public function addNew()
{
echo $this->auth();
}
}
Your code is working fine in codeigniter 4. try removing constructor.

Require has failed in php

I'm new to PHP, I'm trying to require UserController.php from Controller.php but all I get is "HTTP ERROR 500" in browser. What's going on here?
Controller.php
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
require_once "../Controllers/UserController.php";
}
}
UserController.php
class UserController
{
public function __construct()
{
echo '111111111';
}
public function hi(){
echo '1';
}
}
$a = new UserController();
$a->hi();
Class definitions can't be nested inside functions or other classes. So you shouldn't have that require_once line inside a function definition. Move it outside the class.
require_once "../Controllers/UserController.php";
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
}
}
<?php
require_once "../Controllers/UserController.php";
class Controller
{
public function __construct()
{
}
public function call(){
// echo 1;
$a = new UserController();
$a->hi();
}
}

check if function is defined on child class only

<?php
class controller
{
public function view()
{
echo "this is controller->view";
}
}
class home extends controller
{
public function index()
{
echo "this is home->index";
}
function page()
{
echo "this is home-> page";
}
}
$obj= new home;
$method="index";// set to view or page
if(method_exists($obj,$method))
{
$obj->{$method}();
}
?>
my problem :
If we set $method to view, the view() from base controller class will be called.
i want to check if $method exist on home class only
(don't want to check if the function is defined in base class )
any idea how this can be implimented?
Define base class function as private.
Change
public function view()
{
echo "this is controller->view";
}
to
private function view()
{
echo "this is controller->view";
}
It will be work...
EDIT
function parent_method_exists($object,$method)
{
foreach(class_parents($object) as $parent)
{
if(method_exists($parent,$method))
{
return true;
}
}
return false;
}
if(!(method_exists($obj,$method) && parent_method_exists($obj,$method)))
{
$obj->{$method}();
}
This will working perfectly in your case...
Also refer this link
Based off of #vignesh's answer, I needed to use is_callable() to make it work.
abstract class controller {
private function view() {
echo "this is controller->view";
}
}
class home extends controller {
public function index() {
echo "this is home->index";
}
public function page() {
echo "this is home->page";
}
}
$home_controller = new home;
is_callable([ $home_controller, 'view']); // false

How can I change existing function in a class

I have a form class and I want define different functions when form be submitted.
<?php
class Forms {
function __construct() {
if (!empty($_POST['exampleInput'])) {
$this->PostedForms();
}
}
function __call($func, $param) {
}
function PostedForms() {
if (!empty($_POST['exampleInput'])) {
if (function_exists($this->userDefined)) {
$this->userDefined();
}
}
}
}
$form = new Forms();
$form->userDefined = function ($param) {
print_r($_POST);
}
?>
I want define userDefined function outside of class. How can I do this? Can I change any function of class after class was called? Can I change userDefined = function ($param) {print_r($_GET);} for example?

Union results of two functions in php class

I have php class(simple example):
<?php class test{
public function __construct() {
//some code
}
public function __destruct() {
//some code
}
public function echo1 {
//some code
return 1;
}
public function echo2 {
//some code
return 2;
}
}
How could I return results of this two functions echo1 and echo2 in class in one row don't creating two new objects for each function?
$obj= new Test;
echo $obj->echo1().$obj->echo2();
Also, capitalize class names, and you'll need parentheses on those functions:
class Test{
public function __construct() {
//some code
}
public function __destruct() {
//some code
}
public function echo1() {
//some code
return "Hello";
}
public function echo2() {
//some code
return "World";
}
}
$obj= new Test;
echo $obj->echo1()." ".$obj->echo2();
You may have to change your functions a little bit.
...
public function echo1() {
//some code
echo 1;
return $this;
}
public function echo2() {
//some code
echo 2;
return $this;
}
...
Then call it like this.
(new test())->echo1()->echo2();

Categories