0

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

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by kaisarkhan87
0

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.

0

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

0

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();

0
<?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();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.