<?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
Related
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.
I'am begginer in OOP PHP. I have code like this
class Index
{
public $activepage = true;
public $url;
public $page;
function __construct()
{
if ($this->activepage) {
$this->url = "Yes";
$this->page = "Home";
} else {
$this->url = "No";
$this->page = "Index";
}
}
public function show()
{
return $this->page;
}
public function showTest()
{
return "test";
}
}
class Home extends Index
{
function __construct()
{
echo $this->show();
}
}
$page = new Home;
My questions is :
Why I have blank page when I invoke Home class?
But when I change constructor in Home class like this echo $this->showTest();, it works. and displaying "test" on screen.
and what actually diferrent between my show method and showTest method in Index class?
When you add a __construct() in the Home class it overrides the construct from the parent class Index.
You can invoke the parent construct manually with:
function __construct()
{
parent::__construct();
echo $this->show();
}
You can call Parent class method like;
parent::show()
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.
I'm using Yii and I'm new to it.
I have a default main.php layout file and i need to make some data extractions from DB and cookies.
I've written 2 functions:
public function getRegionId() {
if(isset(Yii::app()->request->cookies['region_id'])) {
$sk = Yii::app()->request->cookies['region_id']->value;
settype($sk,integer);
return $sk;
} else {
return 1;
}
}
public function regionId2region($id) {
if(empty($id) or gettype($id)!=='integer') {
return null;
} else {
$reg = Regions::model()->findAll(array(
'condition'=>"alive=1 AND id=".$id,
));
return $reg;
}
}
Now it is not working in any controller. My question is: is it possible to make functions in the layout file or is there a way to pass data to layout file (so that it displays in all controllers)?
Move methods into Regions model and make it static. Or Create Helper class? contains just static methods.
class RegionHelper {
public static function getRegionId() {
if(isset(Yii::app()->request->cookies['region_id'])) {
return (int)$Yii::app()->request->cookies['region_id']->value;
}
return 1;
}
public static function regionId2region($id) {
if(empty($id) or gettype($id)!=='integer') {
return null;
} else {
$reg = Regions::model()->findAll(array(
'condition'=>"alive=1 AND id=".$id,
));
return $reg;
}
}
}
You can use BeforeAction in your controller, like this:
protected function beforeAction($action) {
//Define your variable here:
public $yourVaribale;
//do your logic and assign any value to variable
}
Now, you can use this variable in the view file:
view:
<h1><?php echo $this->yourVariable; ?></h1>
If your functions are located in the controller that calls the view, you could use the $this reference to access the function. Note the public access of the function.
class UserController extends Controller
{
// :
// :
public function fullName($a,$b) {
return $a.' '.$b;
}
}
...and in your view ...
<h1>Test for <?php echo $this->fullName('Tom', 'Jones'); ?></h1>
If the function is in your model, there are a few choices.
class User extends Activerecord
{
// :
// :
public function fullName($a,$b) {
return $a.' '.$b;
}
}
You could pass the model through the render function,
class UserController extends Controller
{
// :
// :
public function actionDisplayView {
$userModel = User::model()->findByPK(1);
$this->render('user_view', array('model' => $model));
}
}
and directly call the function in the view.
< h1 >Test for <?php echo $model->fullName('Tom', 'Jones'); ?>< / h1 >
or, if you did not pass the function, you could call the function in the view (or helper classes). Watch the scope.
class User extends Activerecord
{
// :
// :
// NOTE: You won't have access to $this.
static public function fullName($a,$b) {
return $a.' '.$b;
}
}
and in the view
< h1 >Test for <?php echo User::fullName('Tom', 'Jones'); ?>< /h1 >
Please look at the following code snipped
class A
{
function __get($name)
{
if ($name == 'service') {
return new Proxy($this);
}
}
function render()
{
echo 'Rendering A class : ' . $this->service->get('title');
}
protected function resourceFile()
{
return 'A.res';
}
}
class B extends A
{
protected function resourceFile()
{
return 'B.res';
}
function render()
{
parent::render();
echo 'Rendering B class : ' . $this->service->get('title');
}
}
class Proxy
{
private $mSite = null;
public function __construct($site)
{
$this->mSite = $site;
}
public function get($key)
{
// problem here
}
}
// in the main script
$obj = new B();
$obj->render();
Question is: in method 'get' of class 'Proxy', how I extract the corresponding resource file name (resourceFile returns the name) by using only $mSite (object pointer)?
What about:
public function get($key)
{
$file = $this->mSite->resourceFile();
}
But this requires A::resourceFile() to be public otherwise you cannot access the method from outside the object scope - that's what access modifiers have been designed for.
EDIT:
OK - now I think I do understand, what you want to achieve. The following example should demonstrate the desired behavior:
class A
{
private function _method()
{
return 'A';
}
public function render()
{
echo $this->_method();
}
}
class B extends A
{
private function _method()
{
return 'B';
}
public function render()
{
parent::render();
echo $this->_method();
}
}
$b = new B();
$b->render(); // outputs AB
But if you ask me - I think you should think about your design as the solution seems somewhat hacky and hard to understand for someone looking at the code.