Files
.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
android-development
angular
angularjs
apache
aspnet
bash
blockchain
book-recommendations
bootstrap
bsd-os
bulma
c
canvas
certifications
chef
clojure
cloud-development
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
docker
documentation
electron
elixir
elm
erlang
game-development
gatsbyjs
git
go
groovy
haskell
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
php
ajax
array
arrays
basic-syntax
class
class-inheritance
classes-and-objects
index.md
composer
conditionals
constants
errors
filters
forms
functions
hello-world
if-else-statement
loop
loops
object-oriented-programming
operators
php-array
php-cookies
php-data-types
php-echo-print
php-expressions
php-form-handling
php-form-required
php-forms-url-email
php-functions
php-install
php-keywords
php-operators
php-strings
php-switch
php-syntax
php-syntax-and-comments
php-syntax-overview
php-tags
polymorphism-abstract-interface
pp-sessions
security
sessions
strings
super-globals
switch
switch-statement
switch-statements
syntax
variables
while
working-with-databases
working-with-json-apis
xml
index.md
product-design
progressive-web-apps
puppet
python
r
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
news
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
freeCodeCamp/guide/english/php/classes-and-objects/index.md

55 lines
1.5 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Classes and Objects
---
# Classes and Objects
Classes are the way that we represent types of objects in the world. Objects would be the actual _instances_ of that class in the world. A class defines _properties_ and _behavior_ of an object of that class. The class defines how the object can interact with the rest of the world. Classes also allow us to abstract away details that we don't want to show other people!
Say for example you have a dog named Spot. Spot is one instance of a Dog (class) object.
PHP code to define a class:
```php
// Dog class
class dog {
// Keep name and age private - we don't want to be able to change these!
private $name;
private $age;
// Constructor allows us to make an object of this class with given parameters.
function __construct($name, $age){
$this->name = $name;
$this->age = $age;
echo 'Dog named: '.$this->name.' is '.$this->age.' years old.';
}
// Destructor gets called when the item is deleted.
function __destruct(){
echo 'Dog '.$this->name.' has ran off into the sunset';
}
function getname() {
echo $this->name;
}
function getage() {
echo $this->age;
}
}
$mydog = new dog("Spot", "8");
echo $mydog->getname();
echo $mydog->getage();
```
The code above would echo:
Dog named: Spot is 8 years old.
Spot
8
Dog Spot has ran off into the sunset
I created an object $mydog of class dog. Its constructor was called, I used some methods inside of the class, then the destructor was called.