help a noob with php (similarities between PHP and python)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanolee
Forum Newbie
Posts: 3
Joined: Sun Apr 05, 2015 3:56 pm

help a noob with php (similarities between PHP and python)

Post by ryanolee »

Ok I'm 15 and am very interested in computing :) . I have a acceptable knowledge of OOP. I started with python and came over to html and javascript. after that I came over to php. This is my first time coding a php script an it is not meant to be un-hackable(umg use HTMLentities scrub :( :banghead: ).
The idea behind my code is to create a modular system to create websites using php as a template.(It does not matter if that has all ready been done :? ).
I have had a look over the internet but have not managed to find any "python to php equivalents".
my code is as follows:
(note: I'm using WAMP to execute the code. )

Code: Select all

class website{
	function  __construct($title,$midhead,$body_attr,$scripts){
		$this->$upper="<!DOCTYPE html> \n <html>";
		$this->$head=new head($title,$midhead,$scripts);
		$this->$body=new body($body_attr);
		$this->$lower="</html>";	
	}
	function  return_all(){
	return $this->$upper.$this->$head->return_all().$this->$body->return_all().$this->$lower;
	}
}

class head{
	function  __construct($title,$midhead,$scripts){
		$this->$upper="<head>\n<meta name='RIZZA'> \n<link rel='shortcut icon' href='../img/main.ico'/>";
		$this->$title="<title>".$title."</title>\n";
		$this->$midhead=$midhead."\n";
		$this->$scripts=$this->addscripts();
		$this->$lower="</head>\n";
		}
	function  return_all(){
		return $this->$upper.$this->$title.$this->$midhead.$this->$scripts.$this->$lower;
	function  addscripts($scripts){
		$script="<script src='../js/jwplayer/jwplayer.js'></script>/n<script src='../js/audiojs/audio.min.js'";
		foreach($scripts as $item){
			$script=$script."<script type='text/javascript' src='".$item."'></script>\n";
			}
		return $script;
		}
	}
}
class body{
	function  __construct($body_attr){
		$this->$upper="<body ".$body_attr.">/n";
		$this->$midbody="";
		$this->$lower="</body>";
	}
	function  addcontent($content){
		$this->$midbody=$this->$midbody.content;
	}
	function  addvid($vidsrc,$imgsrc,$id){
		$this->$midbody=$this->$midbody.
		'<div id="'.$id.'">Loading the player...</div>
		<script type="text/javascript">
		jwplayer("'.$id.'").setup({
        file: "'.$vidsrc.'",
        image: "'.$imgsrc.'",
		});
		</script>';
	}
	function  addaudio($src){
		$this->$midbody=$this->$midbody.
		'<p style="visibility: hidden;">  
		<audio src="'.$src.'" preload="none" autoplay loop style=" visibility: hidden;"/>
		</p>';
	}
	function  return_all(){
		return $this->$upper.$this->$midbody.$this->$lower;
	}
}
The questions I have are:
1. have I got it all wrong?
2. Is the "__construct" method the same as the "__init__" constructor method from python.
3. Is "$this" equivalent to "self" in python and dose it need to be passed as an argument into the function.
4. Is the "->" equivalent to that the "." does in python.
5. Is there anything wrong with just doing

Code: Select all

$x="hi"
over

Code: Select all

var $x=hi
6. are all the "$" variables global or they require that to be a variable.
7. how do you recall methods of a class e.g

Code: Select all

$a=new website("hi","","","");
echo $a->return_all()
Thank you in advance :) (any syntax errors e.g missing a semi-colon may be in there but its my first time ever coding in php so be nice :roll: )
ryanolee
Forum Newbie
Posts: 3
Joined: Sun Apr 05, 2015 3:56 pm

Re: help a noob with php (similarities between PHP and pytho

Post by ryanolee »

I'm going to bed will be back in about 10 hours :3
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: help a noob with php (similarities between PHP and pytho

Post by Celauran »

ryanolee wrote:The questions I have are:
1. have I got it all wrong?
2. Is the "__construct" method the same as the "__init__" constructor method from python.
3. Is "$this" equivalent to "self" in python and dose it need to be passed as an argument into the function.
4. Is the "->" equivalent to that the "." does in python.
5. Is there anything wrong with just doing

Code: Select all

$x="hi"
over

Code: Select all

var $x=hi
6. are all the "$" variables global or they require that to be a variable.
7. how do you recall methods of a class e.g
2. Yes
3. Sort of. $this refers to a specific instance. PHP also has self and static to refer to static methods and properties, the latter using late static binding.
4. Yes. $this->callSomeMethod()
5. var is PHP4 style and deprecated. Use public, protected, or private as appropriate.
6. Variables must start with $
7. You've got it right. You would use ClassName::methodName() for static methods.
ryanolee
Forum Newbie
Posts: 3
Joined: Sun Apr 05, 2015 3:56 pm

Re: help a noob with php (similarities between PHP and pytho

Post by ryanolee »

thanks :3
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help a noob with php (similarities between PHP and pytho

Post by Christopher »

In addition to what Celauran said:
ryanolee wrote:1. have I got it all wrong?
Well, your indenting is Pythonesque! See PSR-2 for how PHP should be formatted http://www.php-fig.org/psr/psr-2/
ryanolee wrote:5. Is there anything wrong with just doing

Code: Select all

$x="hi"
over

Code: Select all

var $x=hi
Actually, within code you do not use var, it is just $x="hi". For defining class properties, use public, protected or private to specify visibility.
ryanolee wrote:6. are all the "$" variables global or they require that to be a variable.
$ is required on every variable. The reason is that you can embed scalars in strings like "x=$x" and the variable value is expanded. You can also embed object properties and array elements like this "x={$a->x}, name={$y['name']}".
(#10850)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: help a noob with php (similarities between PHP and pytho

Post by Celauran »

Note that this last requires double quotes. Variables are not expanded within single quotes.
Post Reply