Jared Heinrichs

  • Technology Blog
  • Winnipeg Computer Repair
  • Winnipeg Photographer
  • Cooking With Jared
You are here: Home / Uncategorized / Example of STATIC Properties in PHP

Mar 10, 2009 By Jared Heinrichs Leave a Comment

Example of STATIC Properties in PHP

PHP STATIC property example

Back to OOP Chapter 2

class users
{
	//properties go here
	protected $_thisUser;
	protected $_userName;
	protected $_firstName;
	protected $_lastName;
	protected $_phoneNumber;
	protected static $userNumber = 0;

	//methods go here
	public function __construct($userName,$firstName,$lastName,$phoneNumber)
	{
		$this->_userName = $userName;
		$this->_firstName = $firstName;
		$this->_lastName = $lastName;
		$this->_phoneNumber = $phoneNumber;
		self::$userNumber++;
	}//end of construct

	public function outputUserInfo()
	{
		echo $this->_userName . "\n";
		echo 'First Name: ' . $this->_firstName . "\n";
		echo 'Last Name: ' . $this->_lastName . "\n";
		echo 'Phone #: ' . $this->_phoneNumber . "\n";
		echo 'User #: ' . self::$userNumber . "\n";
		echo "\n\n\n";
	}//end of outputUserInfo

}//End of users class

$users = new users('jheinrichs','Jared','Heinrichs',222-2222');
$users->outputUserInfo();
$users = new users('dheinrichs','Delila','Heinrichs','222-2223');
$users->outputUserInfo();
$users = new users('rheinrichs','Rochelle','Heinrichs','222-2224');
$users->outputUserInfo();

Output Would look like:

jheinrichs
First Name: Jared
Last Name: Heinrichs
Phone #: 415-4085
User #: 1

dheinrichs
First Name: Delila
Last Name: Heinrichs
Phone #: 415-4085
User #: 2

rheinrichs
First Name: Rochelle
Last Name: Heinrichs
Phone #: 477-0956
User #: 3

How would it look without a STATIC Property?

jheinrichs
First Name: Jared
Last Name: Heinrichs
Phone #: 415-4085
User #: 1

dheinrichs
First Name: Delila
Last Name: Heinrichs
Phone #: 415-4085
User #: 1

rheinrichs
First Name: Rochelle
Last Name: Heinrichs
Phone #: 477-0956
User #: 1

Why does it work this way

As you see the user number does NOT increment they way it you might think it should. Every time we created a new “OBJECT” the user number became “0”. In the constructor function it was then incremented to “1”. Going back to when the Property was STATIC the “User number belonged to the WHOLE object. So when we created the 2nd and 3rd users the last value was available.

Filed Under: Uncategorized

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Board Game Rules
  • Camera
  • Computer Hardware
    • Blackberry
    • drivers
    • iPad
    • Magic Jack
    • USB
  • Damn Small Linux
  • Exam Notes
  • Facebook
  • FREE Flashcards
  • Games
    • PC
      • League of Legends
    • Wii
    • xbox 360
  • Music
  • Networking
    • Cisco Certification
    • Mitel
    • Palo Alto Firewall
  • News
    • Google
    • Microsoft
  • Operating System
    • Active Directory (2003)
    • Android
    • Command Prompt
    • Damn Small Linux
    • Group Policy
    • Hyper-V
    • IIS
    • ISA 2006
    • Mac OS X
    • Microsoft Exchange Server
    • Powershell
    • Security
    • SME Server
    • Terminal Server 2003
    • Ubuntu Linux
      • Adito Web SSL VPN
      • OpenVpn-als
      • Webmin
    • Virtual Machine Manager
    • Windows 2003 SBS
    • Windows 2003 Server
    • Windows 2008
    • Windows 2008 R2
    • Windows 2012R2
    • Windows 7
    • Windows 8
    • Windows Command Line
    • Windows Deployment Services
    • Windows Server Backup
    • Windows Vista
    • Windows XP
  • Phones
  • Photography
  • Photos
    • Animals
    • Misc
    • Nature
    • Portraits
  • Portfolio
  • Programming
    • CSS
    • HTML
    • jQuery
    • MySQL
    • PHP
    • Script
  • Programs
    • Acrobat
    • Acrobat Reader
    • Adobe Dreamweaver
    • Adobe Illustrator
    • Adobe Photoshop
    • Anti-virus Software
    • Antivirus
    • Backup Exec
    • Bittorent
    • Blackberry BESADMIN
    • Internet Explorer 9
    • Lightroom
    • Microsoft Office
    • Netbeans
    • Onenote
    • Outlook
    • Shelby
    • Sysprep
    • Trend
    • Video Editing
    • Visual Studio
    • Windows Live Writer
    • WireShark
    • XP Mode
    • Zarafa
  • Recipe
  • Review
  • Software Links
  • Troubleshooting
  • Uncategorized
  • Videos
  • Web Applications
    • Brage
    • Google
    • Spiceworks
    • Wordpress
  • Web Browsers
    • Internet Explorer
  • Web Server
    • XAMPP
  • Winnipeg
    • ISP

Try searching this site!

Copyright © 2021 Winnipeg Web Design