Hi,
Can i ask some helpful tips.I am really confuse about the Class in php.when we really use Class in php? because so far i have not yet coding having Class in my php program.I hope can someone enlighten my mind.
Thank you in advance.
Hi,
Can i ask some helpful tips.I am really confuse about the Class in php.when we really use Class in php? because so far i have not yet coding having Class in my php program.I hope can someone enlighten my mind.
Thank you in advance.
Classes are basically sets of rules (variables and functions - though they’re called properties and methods when referring to objects). Say Class A has a certain structure and Class B has a different structure.
You can create objects which obey these rules. So if:
$One = new ClassA();
$Two = new ClassB();
then $One is an object of type ClassA, and $Two is an object of type ClassB. So $One obeys the rules laid out by ClassA, and $Two obeys the rules laid out by ClassB.
Objects are a little bit like arrays, except that all objects of a given type (e.g. all objects of type ClassA) all have the same property (a.k.a variable) names, and you can run the same methods (a.k.a functions)
So for example:
class Person{
public $Name;
public $Age;
public function sayHi(){
echo "{$this->Name} is {$this->Age} and is saying hi!";
}
}
$Jake = new Person;
$Jake->Name = "Jake";
$Jake->Age = 20;
$Dave = new Person;
$Dave->Name = "Dave";
$Dave->Age = 30;
$Jake->sayHi();
$Dave->sayHi();
$Jake and $Dave are of the same type, but their properties are different. The method sayHi() can be used on both $Jake and $Dave, but when each uses $this->Name, they are referring to the $Name property of the object they are applied to.
It’s a little hard to explain really, but there are plenty of online articles introducing people to OOP - object oriented programming.
Hi,Thank you for this…do you have resources that talks about Class in php so that i can study on it and i can apply it on my program.Thank you so much again for your help,and it really helps me.
Do a search for Object Oriented Programming to learn more about classes and objects. Here are a couple PHP specific ones, though I would strongly suggest reading up on some higher level non-PHP OOP to get a better understanding overall.
Hi, Thank you for this and thank you for your advise.I will get back if i get trouble.thank you.