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();
?>
Related
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 6 years ago.
Improve this question
So, I have this class that gets used over and over again throughout my app. This is just an example, not what's actually needed to be done:
File A:
class Names {
public function first() {
echo 'Bob';
}
}
That file is autoloaded into my app using spl_autoload_register and used all throughout in other pages/classes:
File B:
class LastNames {
public function __construct() {
$this->first = new Names();
}
public function last() {
echo $this->first->first().' Smith';
}
}
$names = new LastNames();
echo $names->last(); // Bob Smith
I have many files that instantiate the class Names inside the constructor.
Does this cause much of a hit on performance?
Should I be using static function instead of public function?
Is there a better way to reuse the functions inside Names over and over again inside different classes?
You have several options, using static function is one of them :
class LastNames {
public static function foo(){
// do stuff
}
}
Or you can use a singleton :
class LastNames {
private static $instance = null;
private function __construct(){
// do stuff
}
public function foo(){
// do stuff
}
public static function getInstance(){
if ( !self::$instance ){
self::$instance = new LastNames();
}
return self::$instance;
}
}
And then you can call it with :
$lastnames = LastNames::getInstance();
$lastnames->foo();
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');
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!
This question already has answers here:
calling class method (with constructors) without object instantiation in php
(6 answers)
Closed 9 years ago.
I have the class people
ex:
class People{
function name($name){
echo "the name".$name;
}
}
how to make a class calling function automatically without having to call the method :
$var=new People ()
please give me the answer?
Add a constructor function:
function __construct() {
$this->name("My name");
}
This does not make a whole lot of sense though, but it's what you asked for :-)
you can do what you want by using a constructor. In php this is done by declaring a method in the class called __construct(). The constructor is run any time the object is created. so in your example
<?php
class People{
public function __construct($name)
{
name($name);
}
function name($name){
echo "the name".$name;
}
}
$var = new People("dave");
the other thing you may be referring to is statics but you do call a method you just dont instantiate an instance of the class
<?php
class People{
static function name($name){
echo "the name".$name;
}
}
People::name("Dave");
this will output "the nameDave"
Every time you create instance constructor is called you just need to explicitly add it in your class.E.g:
class People{
public function __construct() {
echo 'constructing';
}
function name($name){
echo "the name".$name;
}
}
But before start doing actual stuff, I recommend reading: http://php.net/manual/en/language.oop5.php
Edit: From your comments it seems you want to call method statically. If you want to call a method without instantiating you should mark function as static. In your example:
public static function name($name){
echo "the name".$name;
}
And usage:
Person::name('my name');
You are looking for the Constructor.
class Bla {
public function __construct() { // runs when class is instantiated
}
}
Test this:
class Family {
private $myself;
private $mother = '';
private $father = '';
public function __construct($myname) {
$this->myself = $myname;
}
public function printFamily() {
print_r($this);
}
public function setFather($name) {
$this->father = $name;
}
public function setMother($name) {
$this->mother = $name;
}
}
$fam = new Family('George Walker Bush');
$fam->setFather('George Herbert Walker Bush');
$fam->setMother('Barbara Pierce Bush');
$fam->printFamily();
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
}
}