Website Scripts And Cloud Tutorials

How to write simple Class in PHP with examples

Byadmin

Sep 17, 2008

Last updated on May 11th, 2022 at 09:25 am

Click to rate this tutorial!
[Total: 1 Average: 5]

Objects are really just a fancy variable type) with some nifty features that come along with them. To create an object, you’ll need something called a class. Think of a class as the framework behind an object that defined the functionality and variables to keep inside.

Here’s an example

<?php
class myClass {
function myClass(){
}
}
?>

In this case, “myClass” is, well, the name of the class and the word “class” is a keyword that PHP looks at to know what you’re doing. Right inside the class definition, there’s a function, “myClass”. Now, I’m not trying to confuse you on purpose – there’s a reason why it’s named the same as the class. It’s a special kind of function called the constructor. Just like laying the foundation and putting up the walls of a new building is part of constructing it, our “myClass” function is run when the object is first created.

Ah! Now to the fun part! Object creation! (Well, not so much fun as the next step in the process).

So, we have our little sample class above sitting there, ready to use. But, to be able to get to anything inside it, we need to make an object that represents it. Here’s an example:

<?php
$mine=new myClass();
?>

Man that’s hard work…well, okay – so it’s not, but there’s still a little there to explain. The variable “$mine” is the newborn object. If you do a print_r() on it, you can see that it’s a little bit special. To tell PHP that we want to make it an object, we use the “new” keyword along with the name of the class. Our example above doesn’t really do anything yet, but that’s about to change when we introduce properties.

Putting Properties Into the Mix

Like everything else involved with classes, properties are really just something you already know (and love). Properties are just variables in disguise. Why are they different? Well, Inside of a class, you can have variables, but there are some variables that ascribe to a higher calling. These variables can be accessed both inside and outside of the class and are global inside an object.
Want an example? Here’s a simple one:

<?php
class myClass {
var $my_var = 'testing my first class';
function myClass(){
}
}
$mine=new myClass();
echo $mine->my_var;
?>
 

Once you run the above code we will get this output.

In our example above, we’re using the same class structure as before, but we’ve added something different. The “$my_var” variable defined at the top puts that value out where we can get to it later.

Later comes around when, after creating the object, we have some fun with a new operator entering the game, the “->” (or as I usually call it, the arrow). Basically, this tells PHP that the variable you are referencing is a part of the “$mine” object.

PHP automatically pulls the current value from the object and echoes it out just like any other value. This isn’t limited to just variables, either – you can use the arrow to call methods too (functions in a class), like so:

<?php
class myClass {
function echoMe(){
echo 'Hello this is from Mistonline.in';
}
}
$mine=new myClass();
$mine->echoMe();
?>

Output

In the above code we are calling a function within the class as oppose to the previous example in which we called a variable.

Click to rate this tutorial!
[Total: 1 Average: 5]

Leave a Reply

Your email address will not be published.