Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP OOP Tutorial from beginner to advance - (Properties and $this - Part 2)

php oop object oriented properties attributes $this in php oop tutorial

  • Please log in to reply
2 replies to this topic

#1 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 27 September 2012 - 07:35 AM

Hello guys are you doing? This is the part 2 of my PHP OOP tutorials here.
Today we are going to know Properties and the $this in php.. Yay! We are now moving into the PHP coding part.. so would be needing some tools to run your code

You would be needing a web development platform(Apache, php, mysql).. there are two great softwares that I used.

- WampServer
- Xampp

they are what we call a all in one software that includes Apache, PHP, Mysql etc.. or tools that you would be needing in developing a web application.

So you now have a localhost web server.. I suggest that you use a good PHP/HTML/JS/CSS editor that you can use for easy syntax highlighting or if you want to use a notepad.. then go :) but here's a list of good PHP editors

- Notepad++
- SublimeText
- PHP Designer
- Netbeans
- JetBrains PHP Storm
- Adobe Dreamweaver

after you've got everything that is needed.. let's start.

so first let's create a class

class Example {
	 function Sample() {
echo "This is an example";

	 }
}

save the code as example.php

now, what are properties? In my previous tutorial we defined properties or attributes as a characteristic of an object..
so let's add a properties in the class Example


class Example {
public $name;
public $color;

function Sample() {
echo "This is an example";
}
}


this are the properties that we've just added



public $name;
public $color;




yes they are variables...
in php 4 you would do

var $name;
var $color;


but I would suggest moving to PHP 5 because it has lots of new cool functions that you can use :)
those properties or variables can be accessed outside the and inside the class.. because we made them public we are not in the variable scope so I'll do a separate tutorial for that, in the mean time let's know what is the use of

$this

with $this you can call functions and variables of a class. It means this class or this document. In some programming language we call it me. Take a look at this code




class Example {
public $name = "papabear";
public $color;

function Sample() {
echo "This is an example";
}

function SayName() {
echo $this->name;
}

function saySample() {
$this->Sample();
}
}

$obj = new Example();
$obj->SayName();
echo "<br/>";
$obj->saySample();


output :

papabear
This is an example



to be more clear.. $this applies to a class that we are IN. here's another example

class Example {
		 public $name = "papabear";
		 public $color;

	 function Sample() {
				 echo "This is an example";
	 }

	 function SayName() {

				 $name = "alleo";
				 echo $this->name;

	 }

	 function saySample() {
				$this->Sample();
		}
}

$obj = new Example();
$obj->SayName();

Output :

papabear


Take a look at the


function SayName() {

$name = "alleo";
echo $this->name;

}

have you noticed the difference? You can't use a class property or variable if you didn't call them using $this

$name and $this->name is different from each other.. $name is a variable or a property or the function while the $this->name is a global variable/property, please don't be confused with that. I know it's confusing but please.. bare with me :)

That's all for today's tutorial.. Thanks for your time, if you have some questions and found some error in my tutorial please feel free to reply.. I'm still learning this things and wants to share what I've learned.. I'm posting tutorials to learn from critics also :) see ya soon

More Powers
Papabear


Previous Tutorial

Introduction to OOP - Part 1

Next Tutorial

Scope and Calculator
  • 2
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#2 indinity

indinity

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Location:bangladesh
  • Programming Language:C, C++
  • Learning:PHP, JavaScript

Posted 28 April 2014 - 05:18 AM

Thanks a lot!!!!!!!!!!!!!!!!!!!!!!!!

very nice


  • 0

#3 indinity

indinity

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Location:bangladesh
  • Programming Language:C, C++
  • Learning:PHP, JavaScript

Posted 28 April 2014 - 06:03 AM

thanks


  • 0





Also tagged with one or more of these keywords: php oop, object oriented, properties, attributes, $this in php, oop tutorial