Class with PHP

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
anhsoft
Forum Newbie
Posts: 2
Joined: Wed Aug 25, 2004 9:48 pm

Class with PHP

Post by anhsoft »

I am learning PHP, but i don't understand about CLASS in PHP, can you tell me about this, give: example...

thank you !
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

your best bet is to check out http://ca.php.net/language.oop over at php.net.

But basically in a nutshell classes are used to organise and allow you to pass variables from function to function inside the same class

for exmaple

Code: Select all

<?php

	class users
	{
		function loggedin()
 		{	
			return isset($_SESSION['loggedin']);
		}
		
		function displaywelcome()
		{		
			if 	($this->loggedin())
			{
				echo "hello";
			} 
		}
	}

?>
this isnt a very good example, but nonetheless an example. You can access variables from your functions withen the same class by using $this->
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

also, using the above example, to make a new instance of that class you would do

Code: Select all

$user = new users();
but you may need a constructor(users) in the users class to make an instance that way... I'm not too familiar with using classes with php, as I haven't found a need for them yet
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I truly hadn't really needed to use classes until I was developing a script which was pointed out to me that it would run out of memory.
anhsoft
Forum Newbie
Posts: 2
Joined: Wed Aug 25, 2004 9:48 pm

Post by anhsoft »

sorry, iam in Vietnam, i can't read english, i known a little english

have you some code better ...?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the documentation is in several languages: http://www.php.net/docs.php if you can't speak any of those, we probably don't know where to look. try googling..
Post Reply