webbhelp Posted May 22, 2009 Share Posted May 22, 2009 Hi! This is my first post and I am not an expert on english, so... this can be funny 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 . Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted May 22, 2009 Share Posted May 22, 2009 http://www.devarticles.com/c/a/PHP/Object-Oriented-Programming-in-PHP/ Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 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... Quote Link to comment Share on other sites More sharing options...
Yeodan Posted May 22, 2009 Share Posted May 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted May 22, 2009 Share Posted May 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
BobcatM Posted May 22, 2009 Share Posted May 22, 2009 Great explanation ToonMariner, just furthered my knowledge of Objects. Thanks! Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 Thanks and thanks again. Now it feels like a new world has been open in front of me 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 :=) Quote Link to comment Share on other sites More sharing options...
Philip Posted May 22, 2009 Share Posted May 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 Ok, I understand, thanks -------- public function __construct() { $this->setEngine(); $this->setColour(); $this->setWheels(); $this->speed = 0; } Does it start the functions: setEngine() setColour() setWheels() Quote Link to comment Share on other sites More sharing options...
Philip Posted May 22, 2009 Share Posted May 22, 2009 Yes, that's correct, it will run those functions and set the speed to a default of 0 Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 Maybe this is a silly question but I will ask it anyway $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 Quote Link to comment Share on other sites More sharing options...
Philip Posted May 22, 2009 Share Posted May 22, 2009 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']); Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 22, 2009 Share Posted May 22, 2009 No, an object is an instance of a class. A class can be said to be the objects "blueprint". Classes are composed of members (properties and methods). Maybe start with these tutorials and the OOP part of the PHP manual. Quote Link to comment Share on other sites More sharing options...
Philip Posted May 22, 2009 Share Posted May 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 Ok, now I understand, sorry Thanks to you all, for helping, you know... I really appreciate it, thanks Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 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? Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 22, 2009 Share Posted May 22, 2009 Your $object in the example you just posted would indeed be an object. $object->funcame() is calling the method of the object. Quote Link to comment Share on other sites More sharing options...
Philip Posted May 22, 2009 Share Posted May 22, 2009 The variable that holds the class data is the object. Again: $ford = new car; $ford is the object, and car is the class. In other words: $theObject = new theClass Quote Link to comment Share on other sites More sharing options...
webbhelp Posted May 22, 2009 Author Share Posted May 22, 2009 Ok! I thougt I would understand this much earlier but I didn't 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.