Creating and using your first PHP Class

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello,

To start off, you'll need a web server - since we're using a server-side programming language.
Because we're only using the web server for learning, I recommend using XAMPP which can be downloaded from .

You'll need a good text editor which will highlight your code syntax. If you're looking for a free text editor, I would chose Notepad++, which can be downloaded by clicking .
However, if you're wanting a better text editor, you should look into purchasing Sublime Text Editor, which can be downloaded by clicking (there is also a free version which comes with annoying pop-ups asking you to buy it).

Now, once you've got a text editor, create a new document and let's begin.


So, to start off the tutorial we're going to look into building out first class.
To do this, you need to declare a class and give it a name like so.

PHP:
<?php
class myFirstClass
{

}
?>

In my case, I called the class "myFirstClass".

Next, we're going to put something inside of the class.
When you're using variables inside of classes and wish to gain access to them from outside of the class, you need to make the variable a public one.

To make a public variable, just do the following:

PHP:
<?php
class myFirstClass
{
public $myVariable = "Some Value...";
}?>
All done?
Good.

So, let's say we wanted to create two variables inside of the class.
One for your first name and one for your second name.

PHP:
<?php
class myFirstClass
{
public $firstName = "Anna";
public $lastName = "Robinson";
}?>
Well... that's great 'n' all, but how do we access those variables?
Well since the class is an object, we need to create a new one.
This is easily done and should look a little similar to this:

PHP:
<?php
class myFirstClass
{
public $firstName = "Anna";
public $lastName = "Robinson";
}
$obj = new myFirstClass();
?>
Congratulations, you've created and used your first class.
So, how do we get information from the class?

To get information from the class, you need to use the "$obj" to call something from it.
We can do this like so:

PHP:
<?php
class myFirstClass
{
public $firstName = "Anna";
public $lastName = "Robinson";
}
$obj = new myFirstClass();
echo $obj->firstName . " " . $obj->lastName;?>
Now, once you echo the variables from the class, you should be good to go and open it from the browser.

Firstly, you need to save the file in the XAMPP htdocs directory as a .PHP file which in my case, would be found at "C:/xampp/htdocs/".
For the purpose of this tutorial, I'll save it as "tut1.php".

Now open up your favourite browser and navigate over to " " (replacing "tut1.php" with your file name).

You should see in the browser window, something like this.

tut.png




That's it for the first tutorial, I hope you learned something new!
 

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
I haven't learnt PHP much at all, nor will I any time soon. Nice tutorial, this seems to have a similar syntax to Javascript.
 

Users who are viewing this thread

☀️  Switch to Light Theme

Latest posts

Top