Jump to content

Php Class


Err

Recommended Posts

I get the basic concept of classes. But what I don't get it how exactly is it helpful. It's basically a function in a function, there would be no problems in just calling the functions that I need and just completely ignore putting them in a class. I'm trying to get into using classes and I understand it's a big step in OOP.

<?phpclass FormValidator {  function isEmpty($field, $msg) {	$value = $this->_getValue($field);	if (trim($value) == "") {	  $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg);	  return false;	}	else {	  return true;	}  }}?>

Link to comment
Share on other sites

You're right, the concept of classes is a big part of object-oriented programming. And you are also right, it is really a way to organize your functions (or methods if they are in a class). It also makes your code extremely reusable. For example, the code you have there is checking if a field in a form is empty. Now, that is something you do in a lot of applications, so why not put all of your validation functions in one file in a class named Validation. Then when you go to your next project, you just copy the file and you are ready to use your code.

Link to comment
Share on other sites

Note the concept of "organizing functions" is more to do with namespaces and scope resolution. The point of classes is that you can encapsulate relevant data into a single structure, and then operate on that data (by calling the methods), instead of having your data floating all over the place. For example, in the FormValidator class above, when written out fully, you have encapsulated the _errorList field in the class. In this manner, when you program in an object-oriented fashion you are supposed to think about the data you will be working with and what you are going to do with it.

Link to comment
Share on other sites

Think about a class as a type of data structure - it contains data, in the form of fields, and you can operate on that data, using methods.

class Number {		//data	private $n;		//constructor, accessor, mutator	function __construct($n) {		$this->set($n);	}	function set($n) {		$this->n = $n;	}	function get() {		return $this->n;	}		//operators	function double() {		$this->n *= 2;	}	function invert() {		$this->n *= -1; 	}}

$number = new Number(5);$number->double();echo $number->get(); //echoes 10

Link to comment
Share on other sites

I figured the best way to learn this and get all the concepts of this, is to jump straight into it. I'm having some problems with a class I made.

  class maths {	function sum($one, $two) {	  $val = $one + $two;	  return $val;	}  }  class mathsSquared extends maths {	function mathsSquared($one, $two) {	  $val = $this->sum($one, $two);	  return $val;	}  }

  $sum3 = new mathsSquared(6, 6);

I'm getting:var_dump: object(mathsSquared)#1 (0) { } echo: Catchable fatal error: Object of class mathsSquared could not be converted to string in [path] on line 12

Link to comment
Share on other sites

What is line 12?Note:- Constructors can't return anything.- Classes should always start with a Capital letter, by convention.- Constructors in PHP are now named __construct(). (fixed spelling error)Also, remember that one of the main features of classes is that you can store a set of data related to that class.

<?php  class Maths {	protected $one;	  protected $two;	function sum() {	  $val = $this->one + $this->two;	  return $val;	}  }  class MathsSquared extends Maths {	function __construct($one, $two) {		$this->one = $one;		$this->two = $two;	}  }    $maths = new MathsSquared(1, 2);  echo $maths->sum();?>

Link to comment
Share on other sites

Sweet. That's very helpful. Line 12 was just the echo of $sum3. What if I want to call the Maths class itself and just use the sum() there? I noticed it destroyed this functionality on the revised code. Is there a way to keep both?

  $mathsTwo = new Maths;  $sum = $mathsTwo->sum(3,3);

Link to comment
Share on other sites

Well, currently, we are only providing a way to set the values of $one and $two in the MathsSquared class, so we should really define the constructor in Maths instead, and only have things specific to a "squared" version of Maths in MathsSquared. But for fun, we can just have setters on the Maths class:

<?php	class Maths {		protected $one;		protected $two;		function set_one($one) {			$this->one = $one;		}		function set_two($two) {			$this->two = $two;		}		function sum() {			$val = $this->one + $this->two;			return $val;		}	}	class MathsSquared extends Maths {		function __construct($one, $two) {			$this->one = $one;			$this->two = $two;		}	}	$maths = new Maths(); //note - always need brackets	$maths->set_one(1);	$maths->set_two(2);	echo $maths->sum();?>

Link to comment
Share on other sites

  • 5 weeks later...

It modifies the member so that it can be accessed directly from the class without needing to create an instance first.

<?php	class Test {		static $test;	}	Test::test = 5;?>

Link to comment
Share on other sites

  • 3 weeks later...
Check the second link. Any member can be static. You can also make any member, whether it's static or not, public, protected, or private (although it doesn't really make any sense to have a private static property).
couldn't you have a static private property to keep track of a value, and then use a public method to get the value of that private property if necessary? Or maybe you could have a static private property for a database connection, since you normally dont want multiple connections to be created by a script.
Link to comment
Share on other sites

Why not? It all depends on what you need. The process of creating mutator and accessor methods to change and access the values of private members is known as data encapsulation, or data hiding.

Link to comment
Share on other sites

oh ok. i was just confused at first because justsomeguy said it didnt really make sense to have a private static property
That doesn't mean you can't have private static members. It only means it is unnecessary in most cases.That said, I too agree that there is a use for private static members. For example, I have a class that puts errors in a static property (in an array), because errors are common for the whole script's sake. I have a public static method for getting the whole thing. I trust that if anyone is to extend my class, they'd use that property wisely, so I've made it protected. With the same success though, I could've made it private, and make a protected static method "createErrorLogEntry()" that will make sure the error is in the expected format, and do something if it isn't (like throw an exception or something).
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...