Call function across classes through controller [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this controller class.
<?php
class controller{
function __construct(){
$this->db = new db;
$this->output = new output;
}
}
class db{
function get(){
return 'value from db';
}
}
class output{
function view(){
print $controller->$db->get();
}
}
$c = new controller;
$c->output->view();
?>
Ofcourse this is not working, but i think it gets across the idea of what i'm trying to do. How should i do this?

<?php
class controller{
function control($db, $output){
$data = $db->get();
$output->view($data);
}
}
class db{
function get(){
return 'value from db';
}
}
class output{
function view($data){
print $data;
}
}
$db = new db;
$output = new output;
$c = new controller;
$c->control($db, $output);
?>

Here you can use PHP5 Type Hinting. You can read more here
Code:
<?php
class controller
{
function __construct()
{
$this->db = new db;
$this->output = new output($this->db);
}
}
class db
{
function get()
{
return 'value from db';
}
}
class output
{
private $_db;
public function __construct(db $get)
{
$this->_db = $get->get();
}
public function view() {
return $this->_db;
}
}
$c = new controller();
echo $c->output->view();
In this example:
We're type hinting the class of db passing the $this->db instantiated object to the controller class __construct magic method.
And then on output class we're passing the actual classname db with instantiated variable and getting the method get() and storing the value to the private $db_ property and returning to view() method.
Hope it helps!

Related

How to call function in class to function another class in php? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to call a funtion in class to function another class
with example this code:
class test1{
public function a(){
echo "Test";
}
}
class test2
{
public function b()
{
$obj = new test1();
$obj->a();
}
}
what is correct way??
I'm trying to call a funtion in class to function another class with example this code:
I assume you are not being able to call it?
<?php
class test1{
public function a(){
echo "Test";
}
}
class test2
{
public function b()
{
$obj = new test1();
$obj->a();
}
}
$t2 = new test2;
$t2->b();
?>
class test1{
public function a(){
echo "Test";
}
}
class test2
{
public function b()
{
$obj = new test1();
$obj->a();
}
}
$myTest2 = new test2();
$myTest2->b();

PHP parent:: not working? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Why is the following code returning NULL? I am trying to get the parent method to return a value of a property inside the parent class but for some reason it is returning null.
class A {
public $greeting;
public function __construct($message){
$this->greeting = $message;
}
public function getGreeting(){
print 1;
return $this->greeting;
}
}
class B extends A{
public function __construct(){
}
public function getGreetingMessage(){
parent::getGreeting();
}
}
$classA = new A('Hello world');
$classB = new B();
var_dump($classB->getGreetingMessage());
In the parent method "1" is being printed so I know the method is being called
Because the greeting message is not set in class b. so greeting message returns null.
1) return parent::getGreeting();
2) $classA and $classB are different instances. Whatever you set in $classA is not accessible in $classB
3) constuctor of the parent is not called from classB and, moreover, you do not provide any arguments in initialization of classB (look at 2) above)
class A {
public $greeting;
public function __construct($message){
$this->greeting = $message;
}
public function getGreeting(){
print 1;
return $this->greeting;
}
}
class B extends A{
public function __construct($message){
parent::__construct($message);
// or $this->greeting = $message;
}
public function getGreetingMessage(){
return parent::getGreeting();
}
}
$classA = new A('Hello world');
$classB = new B('Hello world 2');
var_dump($classB->getGreetingMessage());
You have to call the parent's constructor in the child class, otherwise the parent constructor is not run and the class isn't properly initialized:
class B extends A{
public function __construct($message){
parent::__construct($message);
}
So you most likely want this
$classB=new B('Hello World');

PHP declare class variable from another instance

I have a hard time figuring out how to add a variable value to an instantiated class in php,
I've been looking at the reflectionClass and tried to return an assigned variable, and now I'm ended up with a getter setter.
I would really appreciate some help, here's an example of my code:
class controller
{
public $models;
private $load;
public function __construct()
{
$this->load = new loader();
}
public function action_being_run()
{
$this->load->set_model('model_name');
}
}
class loader
{
public function set_model($name)
{
{controller_class}->models[$name] = new model();
}
}
The controller class is instantiated without assigning it to a variable, but just:
new controller();
And then the action is executed from within the controller class.
You could pass a reference to $this into set_model()
class controller
{
public $models;
private $load;
public function __construct()
{
$this->load = new loader();
}
public function action_being_run()
{
$this->load->set_model('model_name', $this);
}
}
class loader
{
public function set_model($name, &$this)
{
{controller_class}->models[$name] = new model();
}
}
You also need to change public $model to public $models. There are probably other ways to achieve what you want, by either extending a class or just using magic methods to access the model.
Like this:
class tester{
public function lame(){
return 'super lame';
}
}
function after(){
return 'after function';
}
$tst = new tester; $tst->afterVar = 'anything'; $tst->afterFun = 'after';
echo $wh->afterVar;
echo $wh->afterFun();

How to create public php class object inside a another class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to create public php class object inside a another class?.
I have a class called
page.php
and also
Main.php
and
Content.php
I want to call Main.php and Content.php class inside the page.php class. What is the correct way to do this.
I tried this, but not working :( . Please help.
<?php
class Page
{
public $main;
public $content;
function __construct()
{
$main=new Main_Model();
$content=new Content_Model();
}
public function Menu()
{
$load_menu=$content->Load_Menu();
...
...
}
}
?>
I can only assume you are new to OOP.
But the issue resides within you needing to access the variables from the global scope.
I am also making the assumption that you are using a framework that provides auto-loading and that these classes are actually accessible.
class Page
{
private $main;
private $content;
function __construct()
{
$this->main=new Main_Model();
$This->content=new Content_Model();
}
public function Menu()
{
$load_menu=$this->content->Load_Menu();
...
...
}
}
That should solve everything for you. Also you should define your variables as private unless you plan on exposing them for use in other places as a public interface. And even then there is discussion on using methods to access private variables.
This should fix you're issue and I've changed/fixed a few other bits like public/private variables etc. As others have said you're missing the $this-> in your construct()
<?php
class Main_Model {
public function Load_Menu() {
return "This is the menu function";
}
}
class Content_Model {
public function Load_Content() {
return "This is the main content function";
}
}
class Page {
private $mainmenu;
private $content;
function __construct() {
$this->mainmenu = new Main_Model();
$this->content = new Content_Model();
}
function Menu() {
return $this->mainmenu->Load_Menu();
}
function Content() {
return $this->content->Load_Content();
}
}
$page = new Page();
echo $page->Menu();
echo "<br />";
echo $page->Content();
?>

see variable from other class php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
It's possible to do this if the class B NOT extended the class A but the class A call a new class B
class A{
public $lang;
public function __construct($lang) {
$this->lang=$lang;
}
public function new_B(){
return new B();
}
}
class B{
public function __construct() {
echo 'lang='.A::$lang;
}
}
$root=new A('eng');
$root->new_B();
You seem to have a mixup of concepts here. The $lang property of A is an instance level variable (since it is not defined as static), therefore you cannot access it statically as you are trying to. If you were to declare the variable as static then you would have access to it, but if you have multiple instances of class A that change it, it will change on the class level, rather than instance level.
Is A::$lang common to all A objects you will create? Then make this variable static. If not you can pass A::$lang as parameter to the B constructor. That is
class A{
public $lang;
public function __construct($lang) {
$this->lang=$lang;
}
public function new_B(){
return new B($this->lang);
}
}
class B{
public function __construct($lang) {
echo 'lang='.$lang;
}
}
Following is making A::$lang static:
class A{
public static $lang;
public function __construct($lang) {
self::$lang=$lang;
}
public function new_B(){
return new B();
}
}
class B{
public function __construct() {
echo 'lang='.A::$lang;
}
}
change the class B like this:
class B extends A{
public function __construct() {
echo 'lang='.$this->$lang; // you can use parent variables like this
}
}

Categories