View Full Version : Oop
ggalan
10-24-2010, 10:08 PM
when writing php class is it important that the class name is the same as the file name?
<?php
class Person{
var $name = "Stefan";
function get_name(){
return $this->name;
}
}
?>
should this file be called Person.php?
also, when would you use -> vs = ?
oops, i thought i was in php
class names vs. class file names
no, it's not important. it might help you keep track of what you're doing, however.
-> vs. =
these are very different operators. As you probably know already, the equal sign ( = ) assigns a value to a variable - as in $var = 'value'; or $result = what_some_function_returns(); .
The arrow ( -> ) is used to access properties and methods inside a class. (In OOP, a function inside a class is called a method, and a variable inside a class is called a property.)
Take this, for example:
$person = new Person(); // using "=" to assign the class to a variable
$name = $person->get_name(); // also using "->" to access a method in the class
echo $person->name; // this works
echo $person=name; // this does not
class Person{
public $name = 'Stefan'; // this is a "property" (note: "public" is treated the same as "var")
public function get_name(){
return $this->name;
}
}
ggalan
10-25-2010, 12:37 AM
so if class A would like to get a property in class B, would you recommend using "include" to reference outside classes?
djr33
10-25-2010, 01:50 AM
Include is irrelevant. Include takes the code from one file and "moves" it into another (when processing-- nothing permanent of course). There is no difference between an included bit of code and another bit of code written directly into the page. (The only exception is the __FILE__ constant that will be different based on which file it is used in.)
The big advantage of include is organization-- split up the code into files that can be handled more easily than dealing with one really long file. And it also helps to reuse code in several pages: pages A and B can both include C and thereby share resources.
To do what you are describing, you would need to create an instance of B within A, probably, or reference a global variable (instance of the class). And you also must have the method/variable be public within B, so that A can access it.
Another way to approach this is to create subclasses (extensions) or just make A a bigger class.
The best method really depends on what you're trying to accomplish.
ggalan
10-25-2010, 02:27 AM
so if i had 2 separate class, how would i access 'get_city' from the class Person?
class Person{
public $name = 'Stefan';
public function get_name(){
return $this->name;
}
}
class Place{
public $city = 'd.c.';
public function get_city(){
return $this->city;
}
}
djr33
10-25-2010, 03:40 AM
In their most basic sense, classes are groups of functions (and variables).
Generally speaking, if "get_city" and "get_person" are related functions, you'd put them in the same class.
Your example above is very simple and I don't see any real purpose for it, so I'm not sure what you are trying to test out-- I know you're going to do something else with it, but from that information I can't see an obvious goal.
class Person{
public $name = 'Stefan';
public $home = 'd.c.';
public function get_name(){
return $this->name;
}
public function get_city(){
return $this->home;
}
}
But again, that's not going much for you.
The way that I think about classes is this: if I have a group of related functions that I want to act as a unit, I put them together in a class. And in the "object" sense, another reason is if you have a "thing" that you want to do several things to. For example, you could create a "person" class then make them live somewhere, then echo where they live then make them move to another city. And perhaps they can have children and pay taxes. All of these would be inside the same "person" class. Of course you can relate several classes if you need to, for example if "city" is a complex idea or, as a simpler example, the children themselves are people.
It's kinda like a database with cross-referencing but less about data and more about operations-- functions.
ggalan
10-25-2010, 01:34 PM
i understand that you can group similar methods, the point of my question was 1 class accessing methods and properties from another class and the best was to handle that
couple methods:
1) create an instance of the second class within the first:
class Person{
public $name = 'Stefan';
public $city;
public function get_city(){
$cityclass = new City();
$this->city = $cityclass->get_city();
return $this->city;
}
}
2) create an instance of the second class outside the first:
$cityclass = new City();
$city = $cityclass->get_city();
class Person{
public function get_persons_city(){
global $city;
return $city;
}
}
3) have the city class extend the person class:
class City extends Person{
public $city='Springfield';
public function get_city(){
return $this->city();
}
public function get_name(){
return parent::get_name();
}
}
ggalan
10-25-2010, 03:54 PM
awesome, thanks!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.