Hello
When I normally write classes and then use them, I'd use this:
$site = new Site();
$site->load('header');
But I've seen people do this:
$site = new Site();
$site->load->header();
How do they do it? Confused
Hello
When I normally write classes and then use them, I'd use this:
$site = new Site();
$site->load('header');
But I've seen people do this:
$site = new Site();
$site->load->header();
How do they do it? Confused
In your first example $site->load('header'), you are passing the variable 'header' to your load function.
in the second example the header() is a function being returned from the load call.
It is a matter of how you reference the function load(), in your class object.
I'm curious to see your class care to share an example.
I'll do an example:
<?php
class Site
{
public $var1;
public $var2;
public function __construct($theVar1, $theVar2)
{
$this->var1 = $theVar1;
$this->var1 = $theVar2;
}
}
?>
That's how I write my classes (Just as example)
If I wanted to do the $this->load->header() would I need:
<?php
class Load
{
}
class Header extends Load
{
}
?>
Would this work? :S
no I believe header() would be a function contained in your Load class.
class Load {
function header() {
return "this is the header of the document!";
}
function footer() {
return "the last line of the document";
}
function body() {
return "some body text, some body text blah blah blah";
}
function build_page() {
$top = $this->header();
$body = $this->body();
$foot = $this->footer();
return $top . $body . $foot;
}
}
$l = new Load();
$header = $l->header();
Hey,
Would that mean I can then use:
$this->load->header();
?
<?php
class Site {
include('Load.class.php');
public $var1;
public $var2;
// make a container for your 'Load' class.
public $load;
public function __construct($theVar1, $theVar2) {
$this->var1 = $theVar1;
$this->var1 = $theVar2;
// call a new load object and set your variable here.
$this->set_load();
}
// make a function to set the class load as a variable in your site object.
public function set_load() {
// calling class load
$l = new Load();
// set this objects $load variable
$this->load = $l;
}
// as far as referencing $this->load->header(); .....
public function print_to_screen() {
echo $this->var1 . "<br> \n";
echo $this->var2 . "<br> \n";
// you can use it here to reference what is in header() function in class load.
echo $this->load->header() . "<br> \n";
}
}
class Load {
function header() {
return "this is the header of the document!";
}
function footer() {
return "the last line of the document";
}
function body() {
return "some body text, some body text blah blah blah";
}
function build_page() {
$top = $this->header();
$body = $this->body();
$foot = $this->footer();
return $top . $body . $foot;
}
}
?>
//I imagine on your page you would do something like:
$site = new Site();
echo $site->load->header();
Check this link. it will surly help you to understand every thing about classes:
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.