Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Classes

Featured Replies

Hey everyone!

I need to know if anyone has any really good tutorials out there or know of any good tutorials that teach PHP classes. I am getting pretty decent at PHP but I really can't get past procedure yet.

 

Thank you

I've been getting into OOP PHP lately myself. I've mostly just used the manual. It explains all aspects of it. If I want to go deeper into a topic I just google it.

I also got a book, PHP Bible, which includes just about everything.

 

What PHP version do you use?

PHP4 and PHP5 classes have significant differences.

Like TT, I have lately been trying out my OOP muscles, and to be frank, tutorials are rubbish.

 

Just use the manual to learn the syntax, and write some code.

 

There are a few basic things to remember:

to instantiate a class (create a class):

class MyClass {


}

and to create an instance (a unique version of that object):

$item = new MyClass;

 

Classes have two types of sub bits, namely "properties" or "methods". Properties are variables belonging to that object, and "methods" are just like functions, but only usable by that class:

class MyClass {
var $p1;
var $p2;

function MyFunc() {
	return $this->p1." ".$this->p2;
}
}

 

so now something like this (with that code above):

$item = new MyClass;
$item->p1 = "Richard";
$item->p2 = "Lyon";
print $item->MyFunc();

 

would print "Richard Lyon".

 

I won't go into things like "why OOP is so useful", because I never understood it until I tried it. SO really, just write some code.

 

This page is useful: http://uk3.php.net/oop

  • Author

Alright, thank you guys.

Likewise, I have wanted to start into OOP, I just didn't know where to start.

I also have a book on PHP but it's really only like an intro and most of my other PHP knowledge comes from experience.

So I'll just play around with it and you the manual.

Thank you :D

  • Author

Do you guys have any more tips for classes?

Also, could you take a look at this code and tell me what I am doing wrong or how to improve on it...

It's supposed to take the credentials to the database and then print out a message depending on if it connects or not

 

<?php
class database_connect{
	var $message = '';
	public function _construct(){
		$dbc = mysql_connect('localhost','root','');
		$dbc2 = @mysql_select_db('blog');
		$this->check_connection();
	}
	public function check_connection(){
		if(($dbc != 1) AND ($dbc2 != 1)){
			$message = 'Fatal Error: Not able to connect to database.';
		} else {
			$message = 'Success!';
		}
		return $message;
	}
}
$connect = new database_connect();
$connect->_construct();
print_r($connect);

?>

I think __construct needs two underscores in front. At any rate you can always just rename it to the name of the class to acheive the same thing.

 

Furthermore, a construct is called automatically when you create the class.

 

The main problem however is the you aren't using the properties correctly. If you want to refer to the properties of the object from inside the object then you need to use $this->varname;

 

Also, you don't really need to seperate your methods into public and private classes.

 

Here is a rewritten bit of code for you:

<?php
class database_connect{
var $message = '';
var $dbc = FALSE;
var $dbc2 = FALSE;
public function __construct(){
$this->dbc = mysql_connect('localhost','root','');
$this->dbc2 = @mysql_select_db('blog');
$this->check_connection();
}
public function check_connection(){
if(($this->dbc !== 1) AND ($this->dbc2 !== 1)){
$message = 'Fatal Error: Not able to connect to database.';
} else {
$message = 'Success!';
}
return $message;
}
}
$connect = new database_connect();
print($connect);

?>

 

Note a few other changes:

- changed print_r to print. Whilst it will work, print_r is for outputting arrays. Use print (or echo) to output a string.

- defined dbc and dbc2 at the start of the class.

 

Any idea for you would be find out how to do this:

$connect = new database_connect();
$connect2 = new database_connect("other.server.com","username","password");
print $connect; // localhost
print $connect2; // alien server

- changed print_r to print. Whilst it will work, print_r is for outputting arrays. Use print (or echo) to output a string.

Not quite correct. It's for displaying information about a variable in a way that's readable by humans. It's not exclusive to arrays.

  • Author

Hmm... yeah I will play around try to do it.

I know PHP syntax well - don't get me wrong. Just the OOP aspect of it is the hard part for me.

I'll learn and I'll try the example you gave for me to do.

 

And about print_r, I used it to try to print the object (which I was doing wrong).

I use foreach() for arrays & print() for text and echo() for variables.

fair enough - i didn't know that about print_r.

 

then again the name makes sense: "print readable"...

 

OOP is a bugger to get a hold of, then once you have you wonder why it was so hard...

  • Author

Yeah, I'm sure.

At first, when I started PHP, I felt like it was going to be too hard but I kept at it and now I like it.

I'm sure once I get used to classes, it'll be even better.

Yes, the step from procedural to OOP can be awkward. Currently my site is a bit of a stitch and mix. I now feel I got a better understanding of it, I even see the use of Interfaces and Abstract classes. (dream poly-morphism)

I'm now writing my codebase from scratch in full OOP.

  • Author

Alright, just a side note, I tried your script PP & it's giving me the same result that I got:

Catchable fatal error: Object of class database_connect could not be converted to string in C:\xampp\htdocs\classes\commenting.php on line 21

  • Author

Alright, I got it. Here's the correct code:

<?php
class database_connect{
var $message = '';
var $dbc = FALSE;
var $dbc2 = FALSE;
public function __construct(){
	$this->dbc = mysql_connect('localhost','root','');
	$this->dbc2 = @mysql_select_db('blog');
	$this->check_connection();
}
public function check_connection(){
	if(($this->dbc !== TRUE) AND ($this->dbc2 !== TRUE)){
		$message = 'Fatal Error: Not able to connect to database.';
	} else {
		$message = 'Success!';
	}
return $message;
}
}
$connect = new database_connect();
$connect = $connect->check_connection();
print($connect);
?>

 

CHANGES:

($this->dbc !== TRUE) AND ($this->dbc2 !== TRUE)

instead of them being compared to one

 

- Added

$connect = $connect->check_connection();

  • 1 month later...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.