Jump to content

Trying to get the hang of php classes


louie35

Recommended Posts

Hi,

 

I am trying to get the hang of php classes and I thought the best way is to get your hands dirty :)
I am good at the old php style, functions, array, etc... but when it comes to classes, for some reason i get frustrated and lost :(

Anyway, I have a website that I want to create a mobile section for it and I am doing that using jquery mobile framework, therefore, i created a small class to create the pages on the fly, based on dynamic content or static, trying to save time and writting the same thing over and over again.
Everything works fine, but I have a feeling my class is not ok :( so any suggestion will be appreciated.

 

This is the class:
 

<?php
class create_page{
	var $_header;
	var $_includes;
	var $_footer;
	var $_page;
	var $title;
	var $content;
	var $footer_txt;
	function __construct(){
		
	}
	
	function add_header_mob(){
			$this->_header = '<!DOCTYPE html> 
			<html>
			<head>
			<meta charset="utf-8">
			<meta name="viewport" content="width=device-width, initial-scale=1">
			<title>Mobile Compare Ireland</title>'.$this->include_files().'
			</head> 
			<body>';			
			return $this->_header;
	}
	
	//INCLUDES
	function include_files(){
		$this->_includes = '
		<link href="jquery-m/jquery.mobile-1.3.1.min.css" rel="stylesheet" type="text/css"/>
		<script src="jquery-m/jquery.js" type="text/javascript"></script>
		<script src="jquery-m/jquery.mobile-1.3.1.min.js" type="text/javascript"></script>';	
		return $this->_includes;
	}
	
	//FOOTER
	function add_footer(){
		$this->_footer = '
		</body>
		</html>';
		return $this->_footer;	
	}
	
	//CREATE PAGE CONTENT SHELETON
	function new_page($title,$content,$footer_txt){
		$this->title = $title;
		$this->content = $content;
		$this->footer_txt = $footer_txt;
		
		$this->_page = '
		<div data-role="page" id="page">
		<div data-role="header">
			<h1>'.$this->title.'</h1>
		</div>
		<div data-role="content">'.$this->content.'</div>
		<div data-role="footer">
			<h4>'.$this->footer_txt.'</h4>
		</div>
	</div>
		';
		return $this->_page;
	}
}
?>

... and this is the implementation of it :)

 

<?php include("_funct_header.php");
$mob_page = new create_page();
//add doctype & includes 
echo $mob_page->add_header_mob();

//add content to the page
echo $mob_page->new_page("page1", "content - can be dynamic or static :)", "footer");

//add footer
echo $mob_page->add_footer();
?>

...So what am I missing?

 

Link to comment
Share on other sites

your use of properties is suspect. the point of properties (class variables) are for things that must be referenced from outside of where they are assigned. this includes values that are used in more than one method inside the class and values you want to reference in the scope of where your class is called. none of your properties are referenced outside the method where they are created. you don't need to use a property for every variable. local variables inside of methods are perfectly valid. in the code you posted, there's no need for any of the properties you have shown.

 

the only thing that is dynamic in the code you posted is the content section (though the <title> ... </title> should be.) the rest of your code is just a page layout/template that you have broken up and wrapped class methods around, requiring more code to produce the result. your class methods/properties should be concerned with accepting the input that is dynamic, preforming the processing you need on that input (using htmlentities() on the text you output on a page would be some good processing to include in your code), and returning the result.

 

the main code that uses your class should be only concerned with USING your class and it should be as simple as possible. your main code should not care about things that don't involve information that doesn't change. your header/footer add code is completely static and your main code shouldn't know or care about that. you main code should only be -

<?php include " your class definition file ";
$mob_page = new create_page();
//add dynamic content to the page template and echo the result
echo $mob_page->new_page("page1", "content - can be dynamic or static :)", "footer");
Link to comment
Share on other sites

 

your use of properties is suspect. the point of properties (class variables) are for things that must be referenced from outside of where they are assigned. this includes values that are used in more than one method inside the class and values you want to reference in the scope of where your class is called. none of your properties are referenced outside the method where they are created. you don't need to use a property for every variable. local variables inside of methods are perfectly valid. in the code you posted, there's no need for any of the properties you have shown.

 

the only thing that is dynamic in the code you posted is the content section (though the <title> ... </title> should be.) the rest of your code is just a page layout/template that you have broken up and wrapped class methods around, requiring more code to produce the result. your class methods/properties should be concerned with accepting the input that is dynamic, preforming the processing you need on that input (using htmlentities() on the text you output on a page would be some good processing to include in your code), and returning the result.

 

the main code that uses your class should be only concerned with USING your class and it should be as simple as possible. your main code should not care about things that don't involve information that doesn't change. your header/footer add code is completely static and your main code shouldn't know or care about that. you main code should only be -

<?php include " your class definition file ";
$mob_page = new create_page();
//add dynamic content to the page template and echo the result
echo $mob_page->new_page("page1", "content - can be dynamic or static :)", "footer");

 

I thought it will be good practice to set the variables in advance, even if they are empty values.

 

For the last part, I see your point of the header & footer but this 2 parts contains the main html block of creating a page and also the incuded files needed to make it work.

The reason I used it to create them separate is because in the case of jquery mobile  pages, the could be more than one page id accessed by hash key using ajax and cant have 2 doctype in my code, if you know what I mean.

 

 

<?php include("_funct_header.php");
$page1 = new create_page();
//add doctype & includes 
echo $page1->add_header_mob();

//add content to the page
$page1->title = '<h1>Home</h1>';
echo $page1->new_page("page1",$page1->title, "content - can be dynamic or static :)", "footer");

/* PAGE 2 */
$page2 = new create_page();

//PAGE HEADER & BACK BUTTON
$page2->title = $page2->add_back_btn().'<h1>Header 2</h1>';
//page 2 content
echo $page2->new_page("page2", $page2->title, "Content 2", "Footer 2");

//add closing HTML footer
echo $page1->add_footer();
?>
Link to comment
Share on other sites

Thanks cyberRobot...

Got that and changed it to public instead :)

I'm pretty sure when you don't specify a property the class will infer public. If you're not accessing your variables outside of the class then you want them private.

Link to comment
Share on other sites

good point and thanks again.

 

Find this classes thing really frustrating.

I can do anything with the old functions style and I am pretty good at it, the reason I never moved to the new version :( , but I kind of have to... I suppose.

 

I can also work with classes from a framework, but just never created my own.

Link to comment
Share on other sites

Object Oriented Programming was tough to get a handle on at first.  But then once you get it, it will make development and maintenance SOOOOOO much simpler. Here is simple class that returns yesterdays day and date based on today.  Its silly, but has all the machinery for class and objects. 

<?php
class whenWasYesterday 
{
	public $today;					//class property, assign date input
	public $resultOut;				//method variable for output
	
	public function __construct($t) {               //constructor assigns input to properties
		$this->today = $t; 			//this property now has global scope
	}
	
	/* method to output class result
	calls a private class method with $this 
	which means "this instance" of the class.
	@return string, for output to browser
	*/
	public function returnYesterday() {
		echo "And yesterday was ".$this->calcYesterday();
	}
	
	/* method to calculate yesterday, only the class can modify this.
	@param int, $this->today, timestamp
	@returns string, example Thursday June 2013 
	*/
	private function calcYesterday() {
	  if(!empty($this->today)){
		$resultOut = date('l F Y',($this->today - 86400));
	  }else{
	  	$resultOut = "What happened to the time?";
	  }
	  return $resultOut;
	}
 
}
?>

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<head>
<script javescript>
  //define this moment
  var now = new Date();
</script>
<?php
  //PHP is server side, so this now is actually sooner than the JS now...
  $now = time();
  $phpObj = new whenWasYesterday($now);
?>
</head>
</body>
<p>Today is <script language="javascript">document.write (now);</script><br>
<?php 
  print $phpObj->returnYesterday()."<br>\n";
?>
So there you have it!</p>

</body>
</html>
Edited by rwhite35
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.