Page 1 of 1

Information on ->

Posted: Wed Nov 19, 2003 6:12 pm
by RobertGonzalez
I have seen this syntax used in various PHP scripts and was wondering if anyone can point me in the direction of instruction on proper use, syntax and meaning?

Code: Select all

$var->some_item($some_thing);
From what I recall seeing about this operator, it has something to do with accessing values in an array(?!?), but I am not even sure about that. All help would be greatly appreciated.

Posted: Wed Nov 19, 2003 6:28 pm
by DuFF

Posted: Wed Nov 19, 2003 6:29 pm
by JPlush76
prepare yourself for restructuring your brain :)

Posted: Thu Nov 20, 2003 4:06 pm
by RobertGonzalez
Is object oriented programming a more efficient way of programming? I am new to PHP and quickly beginning to like to functionality. But I have never had programming experience or training, so some of this stuff seems really over my head right now. Is this something that has to be known in order to be a really good programmer?

Posted: Thu Nov 20, 2003 4:09 pm
by JPlush76
there are brilliant programmers who haven't coded a drop of OOP in their life.

It's just another tool for your box. I personally prefer it for large projects, because its main goal is to break it up into easily understandable pieces so our little brains can comprehend :)

I would suggest reading "The Object Oriented Thought Process" published by Sams. Its a good primer on understanding OOP

Posted: Thu Nov 20, 2003 8:03 pm
by DuFF
I too am just getting into OOP programming and I just read a great article that helped me understand and visualize classes, objects and methods a lot better. Check it out here:

http://www.devshed.com/Server_Side/PHP/BackToClass/

Posted: Fri Nov 21, 2003 2:59 am
by JayBird
Have a look at this tutorial on this forum

viewtopic.php?t=8873

Mark

Posted: Fri Nov 21, 2003 6:45 am
by m3mn0n
Once you go OOP, you never go back.

It's just that effective. I'm still not that expereienced at it because I'm still expanding my knowledge of other aspects of PHP, but from the tutorials and books I've read about it, wow does it ever make things simple. ;)

But like JPlush said;
prepare yourself for restructuring your brain
:wink:

Posted: Fri Nov 21, 2003 10:43 am
by mrvanjohnson
The most compelling reason to go OOP initially, at least for me, were duplicate routines. You know those little tasks you do repeatedly throughout your website. Well with OOP you do the bulk of the code in one spot and then reference that code through your site. Then if you ever need to make a change or a tweak, you go to one spot and do it, as oppose to having to go through your entire site.

Again that was the big selling point for me. Now it’s common practice. I said I would only use OOP on big projects but once I got a real useful CLASS file going, now it finds it’s way in just about everything I do. Couldn’t imagine not using OOP in one way or another on a project. Then you graduate to things like PEAR and there’s no turning back at that point :D

Posted: Fri Nov 21, 2003 6:03 pm
by RobertGonzalez
Thank you all for your wonderful feedback. This thread is a great example of the power of this board.

It appears that OOP can really simplify a programmers life (if he could just wrap his brain around it). The tutorials that have been suggested are outstanding. On the off chance this doesn't sound too unintelligent, I am understanding CLASSES and OBJECTS pretty well. What is a METHOD and what does it do?

Posted: Fri Nov 21, 2003 6:15 pm
by Weirdan
Basicaly METHOD is behavior of the objects of some type.

Posted: Sat Nov 22, 2003 7:39 am
by JPlush76
think of a method as just a regular php function but it happens to be inside a class, so they give it a fancy new name called Method :)

so here is a basic class structure

Code: Select all

<?php
class jimclass {

  var $newname;  // this is a variable that will be globally available to the entire class

   function getName()
   {
		$name = htmlspecialchars($this->newname);
		return $name;
   }
	
	function setName($name)
	{
		$this->newname = $name;
	}
	
} // END OF CLASS


$user = new jimclass();

$user->setName('Jim');

echo 'Hi there, my name is '.$user->getName();


}
?>
now lets step through it and explain whats going on...

first line
class jimclass {

that is basically just giving my class a name and defining it as a class

Code: Select all

<?php
var $newname;
?>
that is what is called an attribute that is basically a defining characteristic of what the class is all about. for example if this class was to describe a human being.. a human's attributes might be hair color, eye color, height, weight.

Code: Select all

<?php
 function getName()
   {
		$name = htmlspecialchars($this->newname);
		return $name;
   }
?>
what I'm doing here is returning the name variable with the html tags stripped out of it. maybe this class is used to validate user input so I want to return clean data. as you can see it looks just like a regular function in PHP but in a class its called a "METHOD". Think of it as a Method of getting data, setting data or manipulating data.

Code: Select all

<?php
function setName($name)
	{
		$this->newname = $name;
	}
?>
thats whats called a setter method. basically you NEVER want to let anything outside the class touch your data. In PHP programmers can be lazy about this but its bad practice. The great part about OOP is that you can define who can touch your data. Basically you just want one entry point for setting your data for your class. It makes life alot easier when if something is wrong you can look in just one place.

Code: Select all

<?php
$user = new jimclass();

$user->setName('Jim');

echo 'Hi there, my name is '.$user->getName();
?>
so the first line I fire up my class and I'm assigning it the name $user so now $user is my object

next, I want to set my data(or set my name in this case). This could be a variable of anykind.

then I'm using the METHOD getName() to give me back my name. Notice the use of "->" thats basically saying "getName() is a method of my $user object so dont go looking for it as a function, go look in my class"

I hope that helps a little, its a simple example but getting the basics is key. The class works... so you can copy/paste and try it out.. then play around with it. :wink: