PHP - Real-world Abstract / Interface Examples
I've been searching high and low for some decent (actually, they can be crap, just be REAL) real-world examples of uses of abstract classes and interfaces. I've seen many design patterns, and I get the concept, but now I need to put them into practice.
And I gotta say, I'm absolutely sick of class Animal, class Dog extends Animal, class Vehicle, class Van extends Vehicle examples. Those are CRAP for illustrating real uses of the functionality given to us by OOP. It's one thing to teach a concept, and it's another to put into use within a real application. I can only find the former and the not the latter. Does anyone have any links to, or can they post their own examples of, abstract classes and interfaces in use? Similar TutorialsHello,
Can you guys point me to something where I can follow some tutorial or something and learning CI?
I've already made tons of 'Cars' models... 'Dogs' .. 'Cats'.. etc but I really need some project where I can follow steps to creat and learn while I create.
p.s. I think I post this thread in right place but please correct me if is wrong.
Thank's!
I am seriously trying to wrap my head around abstract classes, but the more I try to understand it the more I get confused.
I've even dug out a book by Larry Ullman to no avail.
Here's the example in the book:
Shape abstract class
<?php # Script 6.1 - Shape.php /* This page defines the Shape abstract class. * The class contains no attributes. * The class contains to abstract methods. * - getArea() * - getPerimeter() */ abstract class Shape { // No Attributes to declare. // No constructor or destructor defined here. // Method to calculate and return the area. abstract protected function getArea(); // Method to calculate and return the perimeter. abstract protected function getPerimeter(); } // End of Shape Class.Triangle class: <?php # Script 6.2 - Triangle.php /* This page defines the Triangle class. * The class contains two attributes: * - private $_sides (array) * - private $_perimeter (number) * The class contains three methods. * - __construct() * - getArea() * - getPerimeter() */ class Triangle extends Shape { // Declare the attributes: private $_sides = array(); private $_perimeter = NULL; // Constructor: function __construct($s0 = 0, $s1 = 0, $s2 = 0) { // Store the values in the array: $this->_sides[] = $s0; $this->_sides[] = $s1; $this->_sides[] = $s2; // Calculate the perimeter: $this->_perimeter = array_sum($this->_sides); } // End of constructor. // Method to calculate and return the area: public function getArea() { // Calculate and return the area: return (SQRT( ($this->_perimeter/2) * (($this->_perimeter/2) - $this->_sides[0]) * (($this->_perimeter/2) - $this->_sides[1]) * (($this->_perimeter/2) - $this->_sides[2]) )); } // End of getArea() method. // Method to return the perimeter: public function getPerimeter() { return $this->_perimeter; } // End of getPerimeter() method. } // End of Triangle Class.and to execute in finding the area and perimeter of a triangle: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Abstract Class</title> </head> <body> <?php # Script 6.3 - abstract.php // This page uses the Triangle class - (Script 6.2), which is derived from Shape (Script 6.1) // load the class definitions: require('Shape.php'); require('Triangle.php'); // Set the triangle's sides: $side1 = 5; $side2 = 10; $side3 = 13; // Print a little introduction: echo "<h2>With sides of $side1, $side2, and $side3...</h2>\n"; // Create a new triangle: $t = new Triangle($side1, $side2, $side3); // Print the area. echo '<p>The area of the triangle is ' . $t->getArea() . '</p>'; // Print the perimeter. echo '<p>The perimeter of the triangle is ' . $t->getPerimeter() . '</p>'; // Delete the object: unset($t); ?> </body> </html>What is the point of having an abstract class when it runs fine without it being declared? For example I can take out extends Shape in the Triangle class and it will work just fine? I don't know if If I will ever understand this concept of abstract and interface classes.... Any help in this matter will be greatly appreciated. Thanks John OK, I fooled around with it some more and took out the include('Shape.php') and it spat an out an error stating it wasn't found. So is an abstract class just forcing a person to adhere to the methods being declared in the abstract class? (I'm probably not explaining this right). Edited by Strider64, 03 October 2014 - 10:55 AM. I can't find an explicit answer on this anywhere, but when you create a child class from an abstract class must you use ALL of the methods that are inside the abstract class in the new child class? Or are these methods just available to the child class to pick and choose?
Can someone help me understand this code... Quote abstract class FormCollection{ protected $fields = $_POST; protected $validator; protected function __construct($validator){ $this->validator = $validator; } } Questions: ---------------- 1.) If an Abstract Class cannot be instantiated, then how can there be Properties? 2.) I guess $fields is supposed to be an array? How do I know that? Isn't there a way to do "type hinting" or whatever? 3.) What data-type is $validator? TomTees Would someone help me out with Abstract Classes and Interfaces... Some questions... 1.) When you have an Interface with Methods, then any Concrete Class that "implements" said Interface must include all of the listed Methods, correct? 2.) When you have an Abstract Class with Properties & Methods, are you required to use all of the listed Properties & Methods similar to how an Interface works? 3.) What is the intent of using Abstract Classes vs. Interfaces? TomTees I was given the snippet below and am trying to figure some things out... abstract class FormCollection{ protected $fields = $_POST; protected $validator; protected function __construct($validator){ $this->validator = $validator; } // and so on... } class RegistrationForm extends FormCollection{ private $formInputs = array(); // and so on... } Questions: ----------------- 1.) If I instantiate the concrete class, RegistrationForm, like this... $form = new RegistrationForm(new Validator()); What does that do to the abstract class, FormCollection? Is it getting instantiated too? 2.) I thought you could NOT instantiate an abstract class like FormCollection? 3.) If you can't, then why is there a constructor in the abstract class like FormCollection? 4.) This code seems wrong... protected $fields = $_POST; I was told by somene that PHP doesn't allow you to assign values to a class method at compile time unless the values come from a constant?! TomTees Hey all - I'm looking for code examples that includes looking for ?passphrase=[something] in the url. If there's no passphrase, give the user a form to fill out which will submit via GET to this url. If the passphrase is correct, tell them so. if it isn't, print incorrect and let them guess again. Maybe some keywords, or a key phrases.. Ideally I can figure it out from there. ? Thanks for any suggestions! I've been using procedural code since I started learning PHP. I hear good thinks about OOP but it looks scary. Does OOP function somehow like CSS? If you have experience with OOP, please share an example for us noobs, where you effectively use it. I am wondering if anyone has some useful PHP socket programming examples. There are a lot of tutorials out there but I find a lot of them are not well written and often are only written from either the client or server's point of view. I would really like to see a good example where two separate scripts are provided. 1. A Server Side to put on the one computer. 2. A Client Side to put on another computer. The best would be something super simple that says/sends... "Hello other world!" and the other side receives it, prints it to a file. This way people could clearly see the interaction and learn rather then geting half the story and trying to puzzle their way through things. Puzzling has is merits but now when you are a busy person. This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=358345.0 I am just starting out with PHP. I have resorted to doing some custom PHP programming because the plug-INs for a CMS I am using need a little customization. The question I have is very simple and I am sure PHP programmers can answer this instantly. I have successfully declared a global PHP variable. All I need to know how to do for now is how to declare a button or a hyperlink or an input form element such that it can change the assignment of this variable. Hello, I use loads of different servers at work and they all run seperate services that I need to keep my stuff running. I want to build a simple webpage that will poll the services and display the current state by colour. EG have table that lists the services in green and have them turn red if the service stops unexpectedly. I can build the tables and stuff, I just don't know how to retrieve the state of a service. I've been messing about with some ideas but nothing is working. eg lets say I have two servers each with one service: service one | IP Address of server | RUNNING service two | IP Address of server | STOPPED Has anyone got any ideas? Thanks!! Hello there, I didn't know where to post this topic. However, I'm curious to know if it's possible to add FTP web interface to our website for clients to login to and access our FTP Server we've setup on our server here at the office? Basically, we want to add a link to our homepage: www.slarc.com that will jump to a login screen and have a web interface where clients can download and upload files to that server? How might I go about this? Is there a script already written, all I found on google was software and remote FTP servers. We want to use our own. Thanks, Lorne I built an application for a client of mine using PHPRunner about 3 years ago. It has functioned perfectly right up to today when they notified me that they were unable to Export any records. I have attached the error report that comes up when you run Export function. According to the report it is unable to find a file or path. I have been on to server with FTP client and all the paths, files and contents are present. Can anyone please give me any advise on what might suddenly cause this error after many years. All other functionality of the Database is fine. Many thanks, Carl. Edited August 1, 2019 by wscwt01 Spelling error Hi,
I installed cakePHH on my win 7 , apache, easyphp laptop.
php files work fine but I cant get anything to run with cakephp which is extracted where I can run html files.
I set fatabase config file, ran index.php and everything seemed fine until i tried to do something
my error is
Missing Controller
Error: UserController could not be found.
Error: Create the class UserController below in file: app\Controller\UserController.php
And I followed
http://www.grasphub....rld-in-cakephp/
Hi,
I am learning PHP and appreciate to share knowledge with other people. I am developing a leave management system for a non-profit organization and I've released the source code : https://github.com/bbalet/lms
Any feedback is appreciated
See you
Edited by bbalet, 22 May 2014 - 01:37 PM. I am getting nothing but my HTML outputted. Here is a screen shot: http://i.imgur.com/yoetz.png I have tested this on three different Apache servers, so I know it is not the settings. I am clueless and not sure why this is happening. Here is some code: <?php include("core/main.php"); //$page = new Page("New", ""); //$page->header(); //$page->footer(); include("core/includes/pages/header.php"); echo "STUFF"; include("core/includes/pages/footer.php"); ?> Hey, thanks guys. I am unsure. I am not really sure on what code I should post. That is the exact code from the screen shot. Please help me solve this riddle! Hello good people of phpfreak. WertySlash is here, I'm a two years graduate of IT here in the Philippines. I wanna become a professional web developer that's why I want to hone those skills I acquired from school and this is one of the way to do it. I'm doing self study through internet, thanks for that person who directed me here. It's a great opportunity to be in a forum like this where I can enhance my knowledge about PHP. So I'm looking forward to expand my php skills from you guys.
Hi,
I'm a London (UK) based Senior Software Developer, over 10 years experience. Primarily interested in software and system design and development for HA environments.
I rally enjoy the challenge of taking an existing system forward from startup mode into something that can grow bigger and better.
Working in High Availability environments. PHP, Nodejs and Golang.
Skillsets..
* Software and System Design
* MySql and SQL Server as admin, programmer and .
* MongoDB, CouchDB, Lucene
* Linux and Windows sysadmin
* Email servers (postfix), built to filter spam/viruses.
* Configuration service designed and built for HA environment
* Zend Framework
* PHP based services inc. HA Email service,
* Javascript. jQuery, Nodejs, Angularjs
APIs inc. Facebook, Twitter, PayPal
Just ask.
Cheers,
Ian
Hey all at phpfreaks!
My name is Matt and I am 26 years old. Currently I run as an Assistant Logistics Lead at a well known chemical production facility. I make okay money. enough to support the family at least, for no degree required(~40k/yr), but loathe my job each and every day. That is partly why I am here!
I am a pretty well-motivated individual, and have finally committed to 'self-teaching' myself programming to find myself a job in my desired career field. I have a little experience in C#, C++, HTML, & CSS, but just the basics. My short-term goal is to learn PHP & SQL and find an entry level backend developer job. At the moment I'm looking for some beginner projects to work on that I ciould build upon and learn from.
Anyways, I suppose that is enough rambling, I'm sure I'll see you all on the forums.
|