The Object of Our Affections
Objects are really just a fancy variable type (as mentioned above) 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 tutorialz {
function tutorialz(){
}
}
?>
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 tutorialz();
?>
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 do know about print_r, don’t you?), 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 tutorialz {var $my_var = ‘testing my first class’;
function tutorialz(){
}
}
$mine=new tutorialz();
echo $mine->my_var;
?>
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’re 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. And this isn’t limited to just variables, either – you can use the arrow to call methods too (functions in a class, remember), like so:
<?php
class tutorialz {
function tutorialz(){
}
function echoMe(){
echo ‘hai this is from tutorialz.tk’;
}
}
$mine=new tutorialz();
$mine->echoMe();
?>
Incoming search terms:
- php class tutorial (4)
- how to use sfValidatorRegex (3)
- why we use marqueeinit javascript (1)
- tell php (1)
- simple php class example (1)
- simple php blog class AND tutorial (1)
- running jsp in easyphp (1)
- php simple class tutorial (1)
- change slider speed anythingSlider c# (1)
- javascrkipt (1)
- how to find duration of youtube video in php (1)
- how to create a search text box in yii framework (1)
- fadeshow sidebar image code css (1)
- excel listbox value return (1)
- Event Table Edit tname1 (1)
- xss with php (1)
You will also be interested in ,
- Exclamation mark (!) at odd places while using php mail fixed
- Export mysql tables to excel new version using phpxls pear spreadsheet writer
- Finding size of a directory using php
- Creating a simple class file using PHP
- Simple XML Reading Using PHP
- Find files inside a directory that starts with a specific string using php
- Simple Visitor Counter Using php
- Set xampp:- Apache Mysql Filezilla Mercury Tomcat as a windows service and get xampp to start automatically on boot up
- Export MySQL to CSV (Excel) using php
- Simple file upload script using php

