Table of contents
If you are reading this, well I am new to the coding journey and I have decided to also try to document and blog about it. so I decided to write about it, I just started learning about php and javascript and I got to the oop part and I got stuck. well, I would be talking about oop in php here, so here let's dive right in and I hope you don't get bored lol.
WHAT IS OBJECT ORIENTED PROGRAMMING TO ME? Object oriented programming is a structured and organized approach in coding, a well-way of writing codes. it is wise to understand oop because it helps in executing large and complex projects. oop is a coding methodology that makes code more modular and reuseable this is how oop is done:
-CLASSES: in oop classes are like blueprints for objects, like building a house you need an architect to lay down the building plans before laying the foundation, It applies in oop also, you need to have a class before you can create an object. These objects have properties and methods and can even have their respective objects that are created from the class. well we know objects are abstract data types yeah?.
classes describe what properties are, and what method an object should have. how do you write a class then; below is an object with a class of User(an empty class that has no properties and methods).
<?php
class User{
?>
}
if you are reading this i hope you are enjoying the flow.
- PROPERTIES AND METHOD: properties in a class just basically describe the object. while methods are functions associated with the object.
<?php
class User {
// the following are properties.
public $username = 'Johnson';
public $email = 'stephen.o@vas2nets.com';
//methods: what the user can do
public function addFriend(){
}
public function postStatus(){
}
}
?>
properties describe the user and method is what the user can do.
LETS DIVE IN MORE we can create a new user, the act of creating a new object in this case, the object is called an instance. An instance is the action of creating a new object(User) based on the original object class(User). for example;
<?php
class User {
}
new User();//an instance, we have created a new user based on the object user.
let's create two instances and add them to a variable:
<?php
class User {
}
new User();//an instance
$userOne = new User();
$userTWO = new User(); // how to know if the above instances if they are based on the class user.
//we use a fucntion called:
// get_class();
echo get_class($userOne);
// we create a new object of a class based on what the class is called using "new", for example, we created a new "class user" using "new"
?>
this is the outcome
from our above code we echo get_class($userOne) and got the class name (User).
if you notice in our previous code I used a keyword called public this is called an access modifier, before I look into that let's look at a keyword called $this and the use of constructors. well, change of mind let's just see what access modifiers are:
<?php
class User {
public $username = 'Johnson';
private $email = 'stephen.o@vas2nets.com';
}
- the > public keyword means that the property $username is accessible in and outside the object.
- the > private keyword means that the property $email is only accessible inside the object.
remember to make more research, this is not a tutorial.
THE $this keyword and constructor function
we can change the values of the properties of the instances, for example, this is the original code with the original properties
<?php
class User {
public $username = 'Johnson';
public $email = 'stephen.o@vas2nets.com';
}
$userOne = new User();
$userTwo = new User();
// accessing the properties with the instance we created
echo $userOne -> username .'<br>' ;
echo $userOne -> email .'<br>' ;
echo $userTwo -> username .'<br>';
echo $userTwo -> email .'<br>' ;
?>
changing the values for userOne
<?php
class User {
public $username = 'Johnson';
public $email = 'stephen.o@vas2nets.com';
}
$userOne = new User();
$userTwo = new User();
$userOne->username = 'Rhoda';
// accessing the properties with the instance we created
echo $userOne -> username .'<br>' ;
the result is the value of the property of the username changing to Rhoda.
yea! try it on $userTwo
$this imagine creating new instances and rewriting the same code repeatedly trying to access property and methods inside the object, well that is stressful yeah? this is were the $this keyword and constructor are really helpful.
check this out;
<?php
class User {
// the following are properties.
public $username = 'Johnson';
public $email = 'stephen.o@vas2nets.com'
// using a constructor;
public function __construct($username , $email){
$this->username = $username;
$this->email = $email;
} //this is a constructor.
//methods: what the user can do
public function addFriend(){
//$this-> username; //this instance of the class, we called the method on userOne and userTwo, so we can say;
return "$this->email , belongs to johnson stephen";
}
}
$userOne = new User('tawer' , 'tawer@vas2nets.com');
$userTwo = new User('mandela' , 'mandelavas2nets.com');
// accessing the properties with the instance we created
echo $userOne -> username .'<br>' ;
echo $userOne -> email .'<br>' ;
echo $userOne-> addFriend();
$userTwo->username = 'judith';//changing values
echo $userTwo -> username .'<br>';
echo $userTwo -> email .'<br>' ;
echo $userTwo-> addFriend();
// using $this. because we called the method on userOne and userTwo, so userOne and userTwo are $this.
//how to find out what properties or method are available on a certain class: use
// print_r(get_class_vars('User')).'<br>';
//get methods on a specific class
// print_r(get_class_methods('User'));
?>
looking at this junk, it is self-explanatory why we use constructors, notice how we didn't need to set an initial property, but using constructors, we could pass the property as parameters and access them from the instances and even change their values.
*imagine cases where we use the access modifier private, where can access the property outside the object, well, as you may know, we use the setters and getters method.
a code snippet showing the getter and setter function.
<?php
class User {
public $username ;
private $email;
public function __construct($username , $email){
$this->username = $username;
$this->email = $email;
}
public function addFriend(){
return "$this->email , belongs to ryu not ru";
}
// getters
public function getEmail(){
return $this->email;
}
//setters
public function setEmail($email){
if(strpos($email , '@') > -1){
$this->email = $email;
}
}
}
$userOne = new User('tawer' , 'tawer@vas2nets.com');
$userTwo = new User('mandela' , 'mandelavas2nets.com');
echo $userOne->setEmail('taweinar@vas2nets.com');
echo $userTwo->getEmail().'<br>';
echo $userOne->getEmail();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>php oop tutorials</title>
</head>
<body>
</body>
</html>
in conclusion, well I guess i just wanted to blog and this might be the last of it as a newbie, i guess i know it takes more than writing but if anyone actually liked this i would continue but if no one did or even read up to the end I quit and just focus on learning how to code. but if you see this, please tell me the truth.
how did do?