Jump to content

Recommended Posts

Hi!

 

This is my first post and I am not an expert on english, so... this can be funny :P

 

I have started with PHP OOP yesterday and it feels like I'm getting better and better but one thing I don't understand is objects.

 

I actually don't know what an object is at all.

 

I create a class:

 

class test

{

 

//and then the object (I think)

 

public obj = 'tjo';

 

}

 

Now the question; That I just wrote, is that an object?

What can I do with object, is it only like variables in an class that got information that I can use in the class?

Or what is an object?

 

Thanks //WebbHelp :)

 

PS. I hope you understand what I was wrote :P .

Link to comment
https://forums.phpfreaks.com/topic/159247-solved-what-is-an-object/
Share on other sites

Thanks for reply, but the thing is that everytime I asked I got links as reply, but I acutally still don't really understand what an object is.

 

class test

{

 

}

 

If you would write an object, i that class, how would you do?

 

maybe it is more easier to understand with code.

 

But thanks you for replying...

 

 

it's something that's hard to explain and even harder to understand

 

I havn't seen any guide that explains it correctly, but I havn't looked at a lot of guides either

 

I'd explain it like this:

An object is a 'thing' that contains variables each containing their own variables

 

Say we have an object 'tree'

tree can contain a variable like 'apple tree'

then the variable apple tree can again contain multiple variables like 'tree size' 'number of leafs' ...

 

I'm not even sure wether this is 100% correct, but it should help you understand it a bit better, it's really hard to understand though

 

 

edit: I actually think the variable 'apple tree' is the object, not the class? that contains apple tree

nice try but not quite there...

 

an object is best visualized as a real life object.

 

lets say we have decided that our every day object is a car. we should all know one of them when we see them...

class Car
{
...
}

Now a car has some things that are common, and engine, wheels, a method to steer and so on. in terms of writing code for that these are properties of the car.

class Car
{
protected $wheels;
protected $engine;
protected $steering;
protected $colour;

....

}

Hope that makes sense so far.

 

Now our car can do things. Accelerate, Stop, crash etc etc. these are methods that our car can perform. These methods all relate to a property our car can have - speed. So lets look at our class.

class Car
{
protected $wheels;
protected $engine;
protected $steering;
protected $colour;
protected $speed;

public function accelerate($rate,$duration)
{

}

public function brake($rate,$duration)
{

}

}

Still with us? reaching for razor blades? good.

 

So now lets look at creating a car. We want a Yellow Reliant Robin with a 750cc engine....

 

class Car
{
public $wheels;
public $engine;
public $steering;
public $colour;
public $speed;

public function __construct()
{
   $this->setEngine();
   $this->setColour();
   $this->setWheels();
   $this->speed = 0;
}

public function accelerate($rate = 0,$duration = 0)
{
   $this->speed *= 1 + ($rate * $duration); 
}

public function brake($rate,$duration)
{
   $this->speed *= 1 - ($rate * $duration);
}

public function setEngine($capacity = 0)
{
  $this->engine = $capacity;
}

public function setColour($colour = 'red')
{
  $this->colour = $colour;
}

public function setWheels($wheels = 4)
{
  $this->wheels = $wheels;
}

public function __toString()
{
  return 'Colour: ' . $this->colour . ' Wheels: ' . $this->wheels . ' Engine: ' . $this->engine . ' litre Speed: ' . $this->speed;
}
}

 

OK so we have a template for creating a car and we can feed in our settings.

 

<?php

$car = new Car();
echo $car;

$car->setColour('yellow');
$car->setWheels(3);
$car->setEngine(0.75);
$car->acclerate(20,2);

echo $car;

?>

 

so we crated a car and by default it set its colour to blue its number of wheels to four its engine to 0 and its speed to 0. But our car then was defined as Yellow, 3 wheeled with 0.75 litre engine and told it to accelerate at 20 metres per second for 2 seconds.

 

the car is now going 40 metres per second....

 

we now have the code to create different cars and all we have to do is...

 

<?php
$robin = new Car();
$robin->setColour('yellow');
$robin->setWheels(3);
$robin->setEngine(0.75);

$porsche = new Car();
$porsche->setColour('black');
$porsche->setEngine(2.5);

echo $robin;
echo $porsche;

$porsche->acclerate(80,4);
$robin->acclerate(20,2);

echo $porsche;
echo $robin;
?>

 

Now I hope from that you can see that a class is a template for an object that we want to use. Once we create that object (instantiation) then we have one copy of the template that we can manipulate.  We can then create another instance for a completely different car using exactly the same code and manipulate them independently.

 

For further reading check out the scope of methods and properties of an object (public, private, protected and static).

 

Hope that was helpful - other feel free to correct typos and what not.

Thanks and thanks again. Now it feels like a new world has been open in front of me :P

 

Almost everything feels right, I understand the most now but I only got some question.

 

 

class Car

{

public $wheels;

public $engine;

public $steering;

public $colour;

public $speed;

 

public function __construct()

{

  $this->setEngine();

  $this->setColour();

  $this->setWheels();

  $this->speed = 0;

}

 

 

What is construct, is that code starting all methods:

 

  $this->setEngine();

  $this->setColour();

  $this->setWheels();

  $this->speed = 0;

 

or what does it do?

 

AND

 

 

public function setWheels($wheels = 4)

{

  $this->wheels = $wheels;

}

 

..eels($wheels = 4)

 

You give wheels value 4

why? Is it only because you want it to have a standard value but then you can give it a new value if you want or?

 

BTW, thanks again, I really appreciate (Difficult word) that you helping me, thanks :=)

A construct is the method (a class function) that is called whenever you create a class (e.g. $porsche = new Car(); ) This allows you to do whatever you want to properties (class variables) like setting defaults and such, or running other methods/functions.

 

 

The setWheels($wheels = 4) is if the user does not give any data when they call that method it defaults to that. So, if I put it setWheels(2) it would give it 2 wheels, but if I put in setWheels() it would give it the default of 4.

Maybe this is a silly question but I will ask it anyway :P

 

$this->speed = 0;

 

If I want to give speed a value = $_GET['speed'];

 

can I write:

 

$this->speed = $_GET['speed'];

 

Or need I do like this:

 

 

class Car

{

public $wheels;

public $engine;

public $steering;

public $colour;

public $speed;

 

public function __construct($speed)

{

  $this->setEngine();

  $this->setColour();

  $this->setWheels();

  $this->speed = 0;

}

 

$class = new Car(10);

 

And then I give the car value = 10?

 

Can I give it a value with $_GET['speed'];

 

You now I am little confused about this, this is the first time I use OOP but you are helping me now and I think I understand it much more now.

 

Thanks :)

You could do:

class Car
{
public $wheels;
public $engine;
public $steering;
public $colour;
public $speed;

public function __construct($speed)
{
   $this->setEngine();
   $this->setColour();
   $this->setWheels();
   $this->speed = $speed;
}
//...
}
$someCar = new Car($_GET['speed']);

Yeah, that's true.

 

I think I understand the most of it now.

 

summary:

 

An object, is like a variable that I can use in the class, like a property for an class.

 

am I right, that object is only for the class, like a variables.

 

Sorry if I ask to much, but this is new for me and I really want to learn it.

 

Thanks :)

A class is a wrapper, or a bucket of variables and functions that are typically have some connection to each other. Variables inside a class are called properties, and functions inside of classes are called methods. In PHP, an object is a variable representation of a class - so when you call $ford = new car;, $ford is the object, and car is the class.

Waite waite waite....

 

I think I have missunderstood all this now..-

 

You mean that the object is:

 

$object    =    new class('tjo');

The object      the properties

 

$object->funcname();

the object a method?

 

Is the variable that I create a new class() in, the object?

 

 

Ok!

 

I thougt I would understand this much earlier but I didn't :P

 

But now I really understand, actually it is like JavaScript/ActionScript.

 

You got the object, then the propertie and then the method.

 

Now I'm quite sure that I understand this.

 

Thank you so much for helping me. Now I finally understand OOP more.

 

THANKS, THANKS, THANKS, THANKS, THANKS, THANKS

 

:)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.