[TUT] OOP Programming [TUT]

AaidenX

Member
Jun 30, 2012
261
29
First off, you must be thinking what OOP means,

O - Object
O - Oriented
P - PHP

So I think you got a rough idea, what it means.

Let's start with a bit of code!

PHP:
<?php
 
?>

Those are the opening and closing codes.

PHP:
<?php
 
class Test //You may use anything instead of test.
{
// Methods and properties are entered here for this class!
}
?>

That is a basic structure of a class. We'll go ahead dumbing the variables.

PHP:
<?php
class Test
{
 
}
$obj = new Test;
var_dump($obj);

So if you execute this, you'll get this display.

PHP:
object(Test)#1 (0) { }

PHP:
<?php
 
class Test {
 
}
$obj = new Test;
 
?>

So in that, we remove the line, var_dump($obj); to stop dumping the variable, we'll be using the echo function to do so.

PHP:
<?php
class Test {
      public $prop1 = "I can be anything you like!"
}
$obj = new Test;

So the above codes add that $prop1 variable which is public and now we'll be showing it in OOP method.

PHP:
<?php
class Test {
    public $prop1 = "I can be anything you like!"
}
$obj = new Test;
echo $obj->prop1

So, if you execute it, your display would be:
PHP:
I can be anything you like!

Give me a like/rep if you find this useful! I'll return like/rep as well! :)
 

Hindi

System.out.println(" ");
Dec 30, 2012
989
193
First off, you must be thinking what OOP means,

O - Object
O - Oriented
P - PHP

So I think you got a rough idea, what it means.

Let's start with a bit of code!

PHP:
<?php
 
?>

Those are the opening and closing codes.

PHP:
<?php
 
class Test //You may use anything instead of test.
{
// Methods and properties are entered here for this class!
}
?>

That is a basic structure of a class. We'll go ahead dumbing the variables.

PHP:
<?php
class Test
{
 
}
$obj = new Test;
var_dump($obj);

So if you execute this, you'll get this display.

PHP:
object(Test)#1 (0) { }

PHP:
<?php
 
class Test {
 
}
$obj = new Test;
 
?>

So in that, we remove the line, var_dump($obj); to stop dumping the variable, we'll be using the echo function to do so.

PHP:
<?php
class Test {
      public $prop1 = "I can be anything you like!"
}
$obj = new Test;

So the above codes add that $prop1 variable which is public and now we'll be showing it in OOP method.

PHP:
<?php
class Test {
    public $prop1 = "I can be anything you like!"
}
$obj = new Test;
echo $obj->prop1

So, if you execute it, your display would be:
PHP:
I can be anything you like!

Give me a like/rep if you find this useful! I'll return like/rep as well! :)

Since you taught classes, SHould have given a brief idea about public,private of the class as well. but nice job
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
im a noob so i dont get the fucking point, why cant i just echo
instead of all that work
You can have more specific classes like User, Messages etc and have methods and properties specific to those classes. Personally don't think this tutorial was thought through proper, I bet it's confused many people who have viewed this who aren't familiar to PHP as all he has basically done is do an echo statement and not explained why it is in a class so it just seems like pointless code when this could literally be done in one line
 

Users who are viewing this thread

Top